php / html使用URL的文本作为变量信息? [重复]

This question already has an answer here:

I have a html page, and php script that is action of a form on the html page. I want to be able to get the last bit of text after the hash of the previous url (from the html page)

www.website.com/htmlpage#token5

So that I get just have the string: "token5" placed into a varible so that when I submit the html form, the PHP script gets the previous URL or something on those lines, to be able to get this string.

something like:

1. submit form from www.website.com/htmlpage#token5
2. action of form goes to www.website.com/phppage
3. php page gets the "token5" string.

How would I go about doing this? thanks`

</div>

You could use JavaScript to get the hash and latter add it to your form element.

  if(window.location.hash) {
      var hash = window.location.hash.substring(1); //Puts hash in variable, and removes the # character
      // do whatever you want to do with hash
  } else {
      // No hash found
  }

Please take a look at:

Can I read the hash portion of the URL on my server-side application (PHP, Ruby, Python, etc.)?

#token5 will never be passed to the server.

What you can do is put the value of #token5 into a hidden input:

<input type="hidden" value="token5" name="token"/>

Then server side PHP depending on how you post your form you can do this:

// If your action on form is 'post'
$token = $_POST['token'];

or

// If your action on form is 'get'
$token = $_GET['token'];

The easiest way perhaps to break a url into it's constituent parts would be to use parse_url

$url='http://www.website.com/htmlpage#token5';
$parts=parse_url( $url );
echo '<pre>',print_r($parts,true),'</pre>';

/* or */

$hash = parse_url( $url, PHP_URL_FRAGMENT );
echo $hash;

Will output

Array
(
    [scheme] => http
    [host] => www.website.com
    [path] => /htmlpage
    [fragment] => token5
)

token5