-7

I try to put document.URL inside PHP code like this following command:

> $string=explode('/',document.URL)

I'm sure it won't work but is there any work around for this? I'm new here!

user3472479
  • 11
  • 1
  • 3
  • 3
    Php is run on the server. Javascript is run in the browser. They are two different locations. So you cant mix php with javascript – Krimson Nov 01 '14 at 19:08
  • You better look for PHP functions that do the same. Try to research before posting a question, most of basic things have already been answered here at SO, and others that you can find in the manual will be negatively received (although there are fast people already answering)... – brasofilo Nov 01 '14 at 19:10
  • Why people try to use php stuff in javascript and javascript stuff in php? I never got this... – Ismael Miguel Nov 01 '14 at 19:10
  • [Then look up how to get the URL in PHP](http://stackoverflow.com/questions/6768793/get-the-full-url-in-php) – Spencer Wieczorek Nov 01 '14 at 19:13

2 Answers2

2

We could do something like making a function that grabs the url (from http://webcheatsheet.com/php/get_current_page_url.php):

function curPageURL() {
 $pageURL = 'http';
 if ($_SERVER["HTTPS"] == "on") {$pageURL .= "s";}
 $pageURL .= "://";
 if ($_SERVER["SERVER_PORT"] != "80") {
  $pageURL .= $_SERVER["SERVER_NAME"].":".$_SERVER["SERVER_PORT"].$_SERVER["REQUEST_URI"];
 } else {
  $pageURL .= $_SERVER["SERVER_NAME"].$_SERVER["REQUEST_URI"];
 }
 return $pageURL;
}

...then we can:

$string=explode('/',curPageUrl());

You can't execute JavaScript to return the page's URL because php is run before the page loads on the server (which is why it's called a server-side language). Even if you could, it would be better to use the language you're executing the statement in.

Cilan
  • 13,101
  • 3
  • 34
  • 51
0

Php is run on the server. Javascript is run in the browser. They are two different locations. So you cant mix php with javascript

You can try this

//Simply call the getCurrentURL() function to get the URL
$string=explode('/', getCurrentURL())

//This function forms the URL you see in your browser
function getCurrentURL()
{
    $currentURL = (@$_SERVER["HTTPS"] == "on") ? "https://" : "http://";
    $currentURL .= $_SERVER["SERVER_NAME"];

    if($_SERVER["SERVER_PORT"] != "80" && $_SERVER["SERVER_PORT"] != "443")
    {
        $currentURL .= ":".$_SERVER["SERVER_PORT"];
    } 

        $currentURL .= $_SERVER["REQUEST_URI"];
    return $currentURL;
}
Krimson
  • 7,386
  • 11
  • 60
  • 97