0

Using window.location.hash (used to pass in ID for page) returns something like the following: Also, for people asking why I used window.location.hash instead of window.location.href is because window.location.href started looping infinitely for some reason, and .hash does not. I don't think this should be a big deal, but let me know if it is and if I need to change it. http://website.com/NewPage.php#?name=1418019307305

[The string of numbers is actually epoch system time]

When using PHP to try to retrieve this variable It is not picking up any text in the file It's supposed to write to.

<?php
$myfile = fopen("File1.txt","w");
echo $_GET['name'];
fwrite($myfile, $_GET['name']);
fclose($myfile);
?>
3
  • 2
    note: those after the hashtag will not be read by the server Commented Dec 8, 2014 at 6:25
  • I had a feeling that might have been why. Commented Dec 8, 2014 at 6:27
  • The problem is that when I use .href instead of .hash, It runs an infinite loop on the URL. Commented Dec 8, 2014 at 6:28

3 Answers 3

1

Try to print $_SERVER variable and it will give you the array and in the desired key you can get the values. It can help you to find that variable in the string.

Sign up to request clarification or add additional context in comments.

2 Comments

If you do a var_dump of $_SERVER in a php script that has a url where the hash tag precedes the query string, you will note that $_SERVER has the variable $_SERVER['QUERY_STRING'] set to an empty string. Furthermore, $_SERVER also does not know about the the hash tag at all. Here is the script that I used <?php echo '<PRE>'; var_dump($_SERVER); in a file called urlhash.php and used the following url to execute the script: localhost/exp/urlhash.php#?name=bla
i was not sure that it will give the #hashtag value or not.but in both case print_r() or var_dump() you can not get the value after that tag.
0

If you want to get the value after the hash mark or anchor, that isn't possible with "standard" HTTP as this value is never sent to the server. However, you could parse a URL into bits, including the fragment part, using parse_url().

This should do the trick:

<?php
    $name_query = parse_url("http://website.com/NewPage.php#?name=1418019307305");
    $get_name = substr($name_query['query'], strpos($name_query['query'], "=") + 1);    
    echo $get_name;
?>

Working example: http://codepad.org/8sHYUuCS

Then you can use $get_name to store "name" value in a text file.

Comments

0

The hash tag is a fragment that never gets processed by the server, but rather the user-agent, i.e. the browser, so JavaScript may certainly access it. (See https://www.rfc-editor.org/rfc/rfc3986#section-3.5). PHP does allow you to manipulate a url that contains a hash tag with parse_url(). Here's another way to get the info:

<?php
$parts = parse_url("http://website.com/NewPage.php#?name=1418019307305");
list(,$value) = explode("=",$parts['fragment']);
echo $value; // 1418019307305

The placement of the hash tag in this case wipes out the query string so $_SERVER['QUERY_STRING'] will display an empty string. If one were to rewrite the url following best practice, the query string would precede the hash tag and any info following that mark. In which case the script for parsing such a url could be a variation of the preceding, as follows:

<?php
$bestPracticeURL = "http://website.com/NewPage.php?name=1418019307305#more_data";
$parts = parse_url( $bestPracticeURL );
list(,$value) = explode("=", $parts['query']);
$hashData = $parts['fragment'];
echo "Value: $value, plus extra: $hashData";

// Value: 1418019307305, plus extra: more_data

Note how in this case parse_url was able to capture the query string as well as the hash tag data. Of course, if the query string had more than one key and value, then you might need to explode on the '&' into an array and then explode each array element to extract the value.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.