I have a requirement to access a sessionID variable passed through header. I need to verify the sessionID before process the request. Also please let us know how can i set a sessionID in request header. please help me to find a solution. Your help is much appreciated. Thank you
I have tried
$headers = apache_request_headers();
foreach ($headers as $header => $value) {
echo "$header: $value <br />\n";
}
Use this code:
foreach (getallheaders() as $name => $value) {
echo "$name: $value\n";
}
This will give all the information of header.
Here is more tutorial about getallheaders() function.
If you want to get only session id then use the below code:
echo session_id();
To set a session you can use,
session_start();
you can use GET method in 1st page to pass the id in header.
session_id($_GET['PHPSESSID']);
session_start();
You can extract the session id in another page by using session_id()
session_id() returns the session id for the current session
else it retuns the empty string, if there is no current session.
<?php
$id = session_id();
if(empty($id)) session_start();
echo "SID: ".SID."<br>session_id(): ".session_id()."<br>COOKIE: ".$_COOKIE["PHPSESSID"];
?>
Hope This helps.
Related
I am maintaining the code for an eCommerce website, they use a highly modified version of osCommerce v2.2 RC2. Was noticing an issue where the session isn't started for a new user until they visit the 2nd page of the site.
Looking at the code, before starting the session, it tries to set a cookie. If it detects the cookie it starts the session. Something along this line:
setcookie('cookie_test', 'please_accept_for_session', time()+60*60*24*30, $cookie_path, $cookie_domain);
if (isset($_COOKIE['cookie_test'])) {
session_start();
...
I found an article here that talks about a situation like this, it states:
The first time you only tell the browser to set the cookie, at the time, there is no cookie data in the request header (which could get from $_COOKIE).
Which explains why it takes two page loads for the session to be started. One to set the cookie and one to get notification from the browser that the cookie is set.
My question is, is there anyway around having to go through two page loads to detect the cookie was successfully set on the users browser?
I found this question that didn't really answer my question completely. The highest voted solution was:
setcookie('uname', $uname, time()+60*30);
$_COOKIE['uname'] = $uname;
Which may make it "work" but it doesn't truely tell me that the script was able to set a cookie successfully.
I also found this question, that suggested accessing the headers_list to find the cookie information like so:
function getcookie($name) {
$cookies = [];
$headers = headers_list();
// see http://tools.ietf.org/html/rfc6265#section-4.1.1
foreach($headers as $header) {
if (strpos($header, 'Set-Cookie: ') === 0) {
$value = str_replace('&', urlencode('&'), substr($header, 12));
parse_str(current(explode(';', $value, 1)), $pair);
$cookies = array_merge_recursive($cookies, $pair);
}
}
return $cookies[$name];
}
// [...]
setcookie('uname', $uname, time() + 60 * 30);
echo "Cookie value: " . getcookie('uname');
Which, again, doesn't seem to be verifying that the cookie was set successfully. All this appears to do is search the headers being sent to the browser for the cookie value.
The only solution I can think of is to redirect on the first visit after setting the cookie. Is there any other way?
Here is the answer:
<?php
function set_cookie($name, $value) {
if (!isset($_COOKIE[$name]) || ($_COOKIE[$name] != $value)) {
$_COOKIE[$name] = $value;
}
setcookie($name, $value, strtotime('+1 week'), '/');
}
// Usage:
set_cookie('username', 'ABC'); //Modify the value to see the change
echo $_COOKIE['username'];
I know how to set PHP cookies in an array but can I clear it without a loop?
For example I'm setting these four cookies
// set the cookies
setcookie("cookie[four]", "cookiefour");
setcookie("cookie[three]", "cookiethree");
setcookie("cookie[two]", "cookietwo");
setcookie("cookie[one]", "cookieone");
// after the page reloads, print them out
if (isset($_COOKIE['cookie'])) {
foreach ($_COOKIE['cookie'] as $name => $value) {
$name = htmlspecialchars($name);
$value = htmlspecialchars($value);
echo "$name : $value <br />\n";
}
}
OUTPUT:
four : cookiefour
three : cookiethree
two : cookietwo
one : cookieone
To clear the cookies, i use the following loop
if (isset($_COOKIE['cookie'])) {
foreach ($_COOKIE['cookie'] as $name => $value) {
setcookie ("cookie[".$name."]", "", time() - 1);
}
}
Are there any way I could clear the cookies without a loop? Thanks in advance
No, you pretty much need to loop through existing cookies as the way to delete/expire a cookie, as you have in your code via setcookie, is one cookie at a time.
Could try
if (isset($_COOKIE['cookie'])) {
setcookie ("cookie", array());
}
or even
if (isset($_COOKIE['cookie'])) {
setcookie ("cookie", array('one'=>'','two'=>'','three'=>'','four'=>'',));
}
if you need the indexes
I need a PHP script that gets a POST request and redirects it to another page as a GET request with all parameters in the URL.
Is this possibile?
You can use the function http_build_query() to generate the GET query string from $_POST.
Afterwards attach it to the redirect URL and use header() with Location for the redirect, for example:
$newURL = 'http://example.com/script.php?' . http_build_query($_POST);
header("Location: {$newURL}");
$URL = "http://thatpage.com/thatpage.php?";
foreach($_POST as $key=>$value) {
$URL +="$key=$value&";
}
then open that $URL page
you need to parse the $_POST variable in order to create a complete GET URL
This question could help you to parse the $_POST array
PHP Parse $_POST Array?
Something like:
foreach($_POST as $k => $v)
{
$getString .= $k . '=' . $v . '&';
}
should format the POSTed variables in the proper format.
I'm receiving values in a GET string from Aweber upon user's submission of a form. I take the variables they send and submit them to a SMS gateway to notify a 3rd party of the submission by text message.
Here's my problem. I need to redirect the page that performs the outgoing SMS commands in a php header to another page that finally displays the GET variables sent from Aweber.
I can retrieve the variables and their values in the first page. How do I pass them to the second page?
Here is the code I'm using on the first page (sms.php) to collect the variables sent by Aweber:
$fname = $_GET['name'];
$femail = $_GET['email'];
$fphone = $_GET['telephone'];
....etc
header('Location: confirmed.php');
exit;
First convert the $_GET HTTP variable into a query string using
$query = http_build_query($_GET);
Then append the query string variable to your redirect header
header('location: domain.com'."?".$query);
Done.
session_start();
$_SESSION['fname'] = $_GET['name'];
$_SESSION['femail'] = $_GET['email'];
$_SESSION['fphone'] = $_GET['telephone'];
....etc
header('Location: confirmed.php');
and get it on the next page like:
session_start();
$fname = $_SESSION['fname'];
$femail = $_SESSION['femail'];
$fphone = $_SESSION['fphone'];
....etc
You don't need to store them in a session, you can easily pass them with your location header:
$fname = $_GET['name'];
$femail = $_GET['email'];
$fphone = $_GET['telephone'];
//now a header with these var's:
header("Location: confirmed.php?name=".$fname."&email=".$femail."&telephone=".$fphone);
In confirmed.php you can get these variables with $_GET method.
Please for anyone reading this in future, use sessions for this kind of variable value transfer because if you rely mostly on adding variable to header then if the user in still on that form and carries out an action that changes the value of the header then your own variable value changes since it depends on the header......simply put, USE SESSIONS.
Store them in the session:
$_SESSION['fname'] = $_GET['name'];
Use session_start at the beginning of each file.
Try this. It worked perfectly for me.
if ($_GET)
{
$query = str_replace("%3D", "=", str_replace("%26", "&", strval(urlencode(http_build_query($_GET)))));
header('location: https://www.example.com'.'?'.$query);
}
else
{
header('location: https://www.example.com');
};
The best you can do is put all your POST variables to a session like this:
On page1.php put:
//Start the session
session_start();
//Dump your POST variables
$_SESSION['post-data'] = $_POST;
And on page2.php put: (If on page1.php we use a normal POST form submit with form action="page2.php")
//Start the session
session_start();
//Access your POST variables
foreach ($_POST as $key => $value) {
${$key} = $value;
$_SESSION[$key] = $value;
}
//Unset the useless session variable
unset($_SESSION['post-data']);
Or on page2.php put: (If on page1.php we use a self submit with form action="<?php echo htmlspecialchars($_SERVER['PHP_SELF']); ?> and then use a header("Location: page2.php"); to move to the page2.php and pass our POST variables via a session)
//Start the session
session_start();
//Access your POST variables
$_POST = $_SESSION['post-data'];
foreach ($_POST as $key => $value) {
${$key} = $value;
$_SESSION[$key] = $value;
}
unset($_SESSION['post-data']);
I literally spent hours figuring that out because all the forums put it wrong or incomplete.
Now it's as easy as just calling the variables you passed from the page1.php like this for example: <b>Points: </b><?php echo $points; ?> and that's it!!
When situating the header('Location: page2.php'); in a if condition, etc. make sure that it will be in the first PHP script of the page and above any HTML output.
This works use this sentex
header('location:member_dashboard.php?id='.$id);
I want to display a link to a user when he has an cookie named abc in his browser
<?php
$cookie = abc;
if (!$cookie)
{
echo <br><center>click here</center></b>;
}
?>
how could I do this?
thanks
The PHP global $_COOKIE could be used to check the user's cookies
<?php
if (isset($_COOKIE["abc"]))
{
echo '<br><center>click here</center></b>';
}
?>
check http://www.w3schools.com/php/php_cookies.asp
You need to check if it is set in the $_COOKIE variable:
<?php
$cookie = abc;
if(isset($_COOKIE[$cookie]))
{
echo <br><center>click here</center></b>;
}
?>
study the php tag $_COOKIE on php.net
and add the "" into the right places.... like: echo "...