Ajax jQuery
Ajax jQuery

Reputation: 345

PHP get request returning nothing

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);
?>

Upvotes: 0

Views: 353

Answers (3)

slevy1
slevy1

Reputation: 3832

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.

Upvotes: 0

Dipak G.
Dipak G.

Reputation: 725

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.

Upvotes: 0

Sachin Shukla
Sachin Shukla

Reputation: 1279

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.

Upvotes: 1

Related Questions