Skip to content Skip to sidebar Skip to footer

$get Doesn't Seem To Work If Not In The Same Page

I'm currently new to php and I'm trying to understand how $GET works across pages. So, in my page 2 I have this code in my link: href='?reward1=true' And in page 1 I have this: if

Solution 1:

$_GET is a variable that is repopulated every time a new HTTP request is made to the server. A "request" is any HTTP interaction done with your application. If you download the html content for the page, and 3 more requests for your php app are issued in that page (images, ajax calls, iframes, etc), that's 4 requests in your php code. If you make 2 AJAX calls to your server, that is still 2 separate requests, even if they were issued from the same loaded document on the same client.

The value of $_GET does not carry across requests -- it is reset each time. For instance, if you access page2.html?foo=bar, and then click on a link <a href="page1.html?baz=quux">page1</a>, the $_GET during the second request will match parameters from the target link, and none of the first.

If your overall goal is to carry state between the requests, you will have to either propagate the parameters from the query string (e.g. reward1) when you generate the link templates to the second page, or keep track of parameters on the server, e.g. inside session variables.

If the first page is only ever directly visited from the second, you might be able to extract the parameter from the previous page by consulting the Referer [sic] HTTP header, but I would recommend that only as a last resort, or as a learning exercise. Referer headers are not always reliable, especially when going from https to http, or when redirects are involved.

Post a Comment for "$get Doesn't Seem To Work If Not In The Same Page"