I want to be able to get hash parameters variable from client side URL and send to server and store in session for echo later in php
URL format in client side Browser:
domain.com/Betatest/#response_uri=https%3a%2f%2flogin.online.com%2ferror%3fcode%3d700AB8&state=email%40domain.com
I want to get the variable of state which is email%40domain.com encoded and uncoded means email#domain.com
I want to get the variable and save to session then echo later the variable using php
Hash values are not sent to the server in a HTTP request. You need to put your data in the querystring, not hash, e.g.
domain.com/Betatest/?response_uri=https%3a%2f%2flogin.online.com%2ferror%3fcode%3d700AB8&state=email%40domain.com
That will give you two parameters (response_uri and state) which you can find in the PHP $_GET array, e.g. $_GET["response_uri"] and $_GET["state"]
Related
How to get the full URL including the string parameter after hash tag? I try to echo
$url = $_SERVER['REQUEST_URI'];
echo $url;
the string after the hash tag wont read.
Pekka's comment should be an answer. The string parameter after the hash tag is not sent to the server, it's for the browsers eyes only.
This means that serverside code (PHP, in your case) does not have this info. The clientside code (the browser, javascript, ...) does.
Ideally,
the part after the ? is info for the server. Put everything your
server needs here
the part after the # is info for the client. Put everything your
client needs here. It's called the Fragment Identifier (Thanks Tim).
Historically, the part after the # was most often used to have your browser quicky scroll to a defined anchor on the page. Nowadays, it is more often used to hold state information for the client.
You could have javascript send this info to the server, or perform different actions based on this info. AJAX is your friend.
The hash (the string including the #) never gets passed to the server, it is solely a behavioural property of the browser. The $_SERVER['REQUEST_URI'] variable will contain the rest however.
If you really need to know what the hash is, you will have to use the document.location.hash JavaScript property, which contains the contents of the hash (you could then insert it in a form, or send it to the server with an ajax request).You can pass up the full URL, including the anchor (the part after the #), using a Javascript onload function that sends that URL to an Ajax endpoint.
You can also take a look here Get entire URL, including query string and anchor
use urlencode() and urldecode() functions
In this short example, I will show you how to pass Hash value to the server and make it redirect to the hash value.
Firstly encode the Hash value in the link button
redirect to Link1
Now to redirect to the link from the server
mylink.php
if ($_GET["redirect"] != null )
{
header("location: urldecode($_GET["redirect"]);
}
I have a URL like:
http://www.example.com/page.php#tabname
The hash will automatically open a specific tab on the page.
I need to work with a _GET variable, and the URL is like:
http://www.example.com/page.php#tabname?color=red
Then on the page, I have:
echo $_GET['red'];
...but I am getting an undefined index error. How do I get PHP to recognize the variable?
Anything after the hash is not sent to the server. Regardless, you should probably format your url so you send the GET parameters correctly.
http://www.example.com/page.php#tabname?color=red
should be
http://www.example.com/page.php?color=red#tabname
You need to put the query string before the hash:
http://www.example.com/page.php?color=red#tabname
How can I identify this type of tags:
example.com/file.php#inbox or example.com/another.php#spam
Like Gmail is detecting #inbox #spam #mycontent
I have tried with $_SERVER['REQUEST_URI'] but it does not work...
The hash part (everything after and including the # character) is not sent to the server. Only the client side (javascript) can read and use it.
The hash property can be requested from the Javascript location objects: location.hash.
If you want to pass data to a page, use GET parameters:
page.php?key=value
In PHP, you can use $_GET['key'] to retrieve value.
I have a doubt about how I can check an URL like this:
http://your.url/set_new_password.php?userid=564979&code=54c4a2767c2f485185ab72cdcf03ab59
I need to check if the userid exists in the database and if the userid is associated to the hash in the link.
I Read that it is not possible check the url within php. If so, is it possible to solve this problem? I need to verify if the hash and userid present in the link exist in the database.
Any other alternatives?
The variables userid and code in the URL are made available to PHP in an array called GET:
echo $_GET['userid']; // 564979
If you have a hash (or fragment) in your URL, this won't get back to PHP:
www.mysite.com?val=1#part2
In the above, PHP can see the domain and the val variable, but not #part2. Sites that use the hash to significantly change the page (eg GMail) use javascript to pull in new content when the hash changes.
Be sure to sanitize your variables before using them, to avoid malicious users being able to hack into your system. This is a big topic, but read up on the following:
Data Filtering
PHP Data Objects
Sanitize and Validate Data with PHP Filters
If you don't sanitize, someone could change your url so that the variable is set to:
;DELETE * FROM mytable;
When you query your db without sanitising your inputs, you could lose all your data.
use the $_GET variable in php
$_GET['userid']
see tutorial here
In PHP, the $_GET array has the url parameters. So in this case, you'd use $_GET['userid'] and $_GET['code']
See Server consist of apache , php , mysql . When you access this url through your browser it is first send to apache which forwards your request to php . Php takes full controle from there on . A request made by client browser consist of various data which can be divided into types cookies , headers , post , get request . All these data can be access in there respective suprglobal variables in php $_GET , $_POST and so on . In your case you need to access $_GET . so do $_GET['userid'] to access userid , and $_GET['code'] to access code . Lastly you would connect ot MYSQL and do querly like "Select * from users where 'userid' = $_GET['userid'] and 'code' = $_GET['code'] " ;
When I use getJSON in JQuery to an external domain the request that is made does not include cookies for that domain. I'm using this for my analytics script that I am writing and I need to set a cookie on the external domain where the script is running so I can track unique visitors.
The files
domain1.com/website.html
<script src="http://domain2.com/tracker.js"></script>
domain2.com/tracker.js
//Get information about the user
info = "(here's some things about the user)";
//Send data using JSON
$.getJSON("http://domain2.com/getdata.php?"+info,
function(data){}
);
domain2.com/getdata.php
/******
* Code to save data and stuff
*******/
//Get the current cookie (if any).
$current_tid = $_COOKIE['tID'];
//checks if the cookie is a string of 50 characters
if (strlen($current_tid)==50){
$TrackerID = $current_tid; //If the cookie already have a unique string, then use it!
} else {
$TrackerID = random_gen(50); //Generates a new random string with 50 characters
}
//Set cookie "tID" with the unique variable $TrackerID
setcookie("tID",$TrackerID,time()+60*60*24*365);
So, the thing is that when the user loads website.html on server1, the user also loads tracker.js on server2 which sends some data with JSON to getdata.php. However, the script does not send cookies and getdata.php will generate a new string every time the script is loaded.
Is there any way to send cookies using JSON?
You should use JSONP instead of regular JSON:
In you script you should add this:
$.getJSON("http://domain2.com/getdata.php?callback=?&"+info,
function(data){}
);
And instead of the original JSON, you PHP script should return your JSON in the format:
header("Content-Type: text/javascript");
$callback = $_GET["callback"];
print "$callback(";
// Code to produce the JSON output as normal
print ");";
More info on JSONP and jQuery is available here.
In my experience, allowing / disallowing 3rd party cookies is a security setting in the browser, which the latest safari blocks by default (3rd party cookies).
http://www.willmaster.com/library/cookies/setting-a-cookie-on-a-remote-domain.php
http://www.bobulous.org.uk/misc/third-party-cookies.html
You could try:
1) Send your tid from www.domain2.com include, and use the js to set this tid value on a cookie stored on www.example1.com.
2) When including your tracking script, update it to send across the TID stored in www.example1.com's cookie, as a parameter for the include.
This way the cookie is set on www.domain1.com (so wont get blocked be default). You just need to write a funky bit of JS to send the www.domain1.com TID cookie value as a parameter to the tracking script on www.domain2.com, if the cookie value exists on www.domain1.com.