I have an app running on localhost/index.php
In Javascript I use a History API: history.replaceState({}, null, "?uid=" + uid);
to change the URL of my page (uid
is a variable with randomly generated value). After this manipulations address was updated to localhost/index.php?uid=dqw12312aws
, for example.
Then I try to receive a uid
variable on server side by: console.log('<?php echo "$_GET[uid]"; ?>');
.
But I obtain an unexpected value.
My first request to localhost/index.php?uid=dqw12312av
- $_GET[uid]
will be an empty string.
But my second request to localhost/index.php?uid=qwtgas23123
for example - $GET[uid]
will be a value of my first request and equal dqw12312aws
and so on. Why is this happening?

- 1,615
- 12
- 29

- 9
- 5
-
3The entire point of `history.replaceState` is that it *does not* load a new page from the server. So running code on the server to detect will not work. – IMSoP Apr 28 '14 at 19:15
-
And what should I do, if I want to change my URL without page reloading, and transmit `var uid` to PHP via GET method. – orangeeez Apr 28 '14 at 19:45
-
^^ Use `history.replaceState` to change the variable and then ***AJAX*** it back to the server... – War10ck Apr 28 '14 at 20:03
1 Answers
You are using console.log('<?php echo $_GET["uid"]; ?>');
in javascript, correct?. Javascript is not on the server side. Javascript get interpreted by the browser. The PHP is interpreted by the server, I.E. PHP is server side. Server side means that the code only gets interpreted when the server side-file is run by the server. (I.E. when the page is loaded).
When you load the PHP file you basically ask the server to go into the file, find any PHP and execute it.
This means, in this case, that $_GET['uid']
will contain the uid
url-param that was set when the page was loaded. So
$_GET['uid']
will not change when the URL is changed dynamically via javascript. Because:
The page isn't refreshed → The PHP file does not run → PHP code doesn't get evaluated again.
You could solve this by retrieving the URL-query string by using javascript.
E.G.
function getUrlVar(requestedKey) {
var hash;
var hashes = window.location.href.slice(window.location.href.indexOf('?') + 1).split('&');
for (var i = 0; i < hashes.length; i++) {
hash = hashes[i].split('=');
var key = hash[0],
val = hash[1];
if (key === requestedKey) {
return val;
}
}
return false;
}
var uid = getUrlVar("uid");
If you have this URL in the browser: http://localhost/index.php?uid=1234
Then the uid
variable in the code above contains 1234
In a comment below you asked me how to send a get variable to PHP.
To run a PHP-script without refreshing the page, you could use dynamic requests. E.G. ajax.
If you are new to web-scripting and javascript, I recommend you take a look at jQuery and jQuery.ajax
So you would include jQuery in your index.php and run this JS code:
function getUrlVar(requestedKey) {
var hash;
var hashes = window.location.href.slice(window.location.href.indexOf('?') + 1).split('&');
for (var i = 0; i < hashes.length; i++) {
hash = hashes[i].split('=');
var key = hash[0],
val = hash[1];
if (key === requestedKey) {
return val;
}
}
return false;
}
function sendUidToPHP(){
$.post("receiver.php", {uid: getUrlVar("uid")}, function(data){
// The data argument now contains the response from php
// To learn more about this see http://api.jquery.com/jquery.ajax
// and http://api.jquery.com/jquery.post
});
}
The receiver.php would be able to read the uid
from $_POST['uid']
.
One more thing. If the uid
URL-param is the only thing needed to log in to another user, then your script has a HUGE security flaw. Take a look at PHP-Cookies and PHP-Sessions

- 1
- 1

- 1,615
- 12
- 29
-
1
-
It's a modified code i got from another question. I will clean it up and add source. Thanks for pointing it out! – Andreas Storvik Strauman Apr 28 '14 at 19:17
-
This is not exactly what I need. I want to transmit an `uid` variable in JS to PHP over GET method. – orangeeez Apr 28 '14 at 19:37
-
Then you should clarify, and ask for that in your question. I have extended the answer pretty much. Maybe you will understand how it works by reading it now :) – Andreas Storvik Strauman Apr 28 '14 at 19:45
-
The bottom part now contains how to send the `uid` from javascript to PHP. – Andreas Storvik Strauman Apr 28 '14 at 19:54
-
@AndreasStorvikStrauman Assuming the OP is using ajax. This may not be the best assumption, as it was not tagged with jQuery in the question... – War10ck Apr 28 '14 at 20:04
-
I am not assuming he is. Did you even read my answer? I added that part as a request from him in the comment right above yours. – Andreas Storvik Strauman Apr 28 '14 at 20:06
-
1@AndreasStorvikStrauman Thanks for so meticulous clarifications. This example is useful for me. I didn't want to refuse from GET it seems more gracefully but it turned non-working. – orangeeez Apr 28 '14 at 20:06