PHP: Passing variable with cookie - php

What's the correct way of passing variable using PHP cookie. I can't seem to get it to work? I keep getting "FAILED!"
Here's my code:
On 1st page:
$crpid['ONE']="PAGE1";
$crpid['TWO']="PAGE2";
$crpid['THREE']="PAGE3";
$crp_id = $_SERVER["REDIRECT_URIPART"];
$crp_value = $crpid[$crp_id];
session_start();
setcookie('crpid', $crp_value, time()+3600, "/");
On 2nd page:
if(!isset($_COOKIE['crpid']) && $_COOKIE['crpid']==''){
echo "FAILED!";
}
else{
echo "Cookie ".$_COOKIE['crpid']." is set!";
}

Related

How to match $_SESSION with if..else?

Here i have an if else that create cookie with name check_CookiesRW and with content randomly generated by rand(1000,999999);. I would love to check if the cookie content randomly generated and stored on $_SESSION would match and echo out Cookie is SET something like this
if(isset($_COOKIE['check_CookiesRW']) && $_COOKIE['check_CookiesRW'] === $_SESSION['cookie_content'])
how can i achieve this without it throwing out error Warning: Undefined variable $_SESSION ??
<?php
if(isset($_COOKIE['check_CookiesRW'])) {
print "<h1>Cookie is SET</h1>";
} else {
$cookie_name = "check_CookiesRW";
$cookie_content = rand(1000,999999);
$_SESSION['cookie_content'] = $cookie_content;
setcookie($cookie_name, $cookie_content, time()+30, "/");
print "<h1>Created Cookie</h1>";
}
?>

Correct Unset Cookie method in PHP with MYSQL

I'm setting a Cookie with the following code:(admin.php)
if ($_POST['stayLoggedIn'] == '1') {
setcookie("id", $row['id'], time() + 60*60*24*365);
}
header("Location: addtip.php");
I can't get the cookie to unset, I've searched the site and the following code should be correct but it's not working;(admin.php)
if (array_key_exists("logout", $_GET)) {
unset($_SESSION);
setcookie("id", "", time()-60*60);
$_COOKIE["id"] = "";
}
Testing the cookie has been unset using the following code on the "loggedinpage" which would return to the admin login page if cookie was unset (addtip.php)
session_start();
if (array_key_exists("id", $_COOKIE)) {
$_SESSION['id'] = $_COOKIE['id'];
}
if (array_key_exists("id", $_SESSION)) {
echo "<a href='admin.php?logout' class='btn btn-danger btn-logout'>Log Out</a>";
} else {
header("Location: admin.php");
}
The problem is that you aren't clearing the $_COOKIE['id'] value correctly. You are setting it to an empty string. The idea is correct, but you have to use unset() to remove the entry from the $_COOKIE array. If you don't do that, the if() condition array_key_exists("id", $_COOKIE) will result in true even though there is no any usable value in it. And setting the $_SESSION['id'] with an empty string as well would make the following if() condition array_key_exists("id", $_SESSION) result in true as well. Therefore you get the logout link.
if (array_key_exists("logout", $_GET)) {
unset($_SESSION);
setcookie("id", "", strtotime('-1 year')); // send a header to remove the cookie
unset($_COOKIE["id"]); // remove the cookie for the remaining CURRENT http request
}
Not sure if unset($_SESSION); is the right thing to do, you might want to use session_destroy(); instead/additionally.

how to pass session value other page if condition true in php

i have a $_sesstion['usermail']. i want to pass this value to next page.if condition match ($answer= $_SESSTION['usermail']);
if(isset($_POST['compair']))
{
echo $_SESSION['question'];
$_SESSION['usermail'];
$answer=$_POST['answer'];
if ($answer == $_SESSION['answer'])
{
header("Location:resetpass.php");
}
else
{
echo "<script>alert('Please Try again')</script>";
}
}
i want to pass $_sesstion['usermail'] value on resetpass.php page.
I think your logic is wrong here. What exactly are you checking in the if statement. A session variable means you can use it on every page that has session_start(); on top.
Sessions by default pass to other pages.
Make sure you have start_session(); on top of the page you want to access the session variable.
So if $_SESSION['usermail'] is working on your current page, it'll work on your next as well with same data.
Get an idea from this exmple
First Page
<?php
session_start();
$_SESSION['name'] = "Adam";
?>
Second page
<?php
session_start();
echo $_SESSION['name'];
?>
You can use GET methods for sharing your session value to next page...
if(isset($_POST['compair']))
{
echo $_SESSION['question'];
$_SESSION['usermail'];
$answer=$_POST['answer'];
if ($answer == $_SESSION['answer'])
{
$value_to_share=$_SESSION['usermail']; // You can share using GET
header("Location:resetpass.php?value=$value_to_share");
// receive this value at resetpass.php by $_GET['value']
}
else
{
echo "<script>alert('Please Try again')</script>";
}
}

Add session in wordpress

I want to add session and retrieve value from it in wordpress. I am using subdomain for my website. I have added code in header.php file of a theme to retrive a subdomain and put it in session. I have started session and put the value in it. When I use subdomain in url session retrives session value but without subdomain name, it doesn't works, even session have that value. Following are my code:
$url = $_SERVER['SERVER_NAME'];
$parsedUrl = parse_url($url);
$host = explode('.', $parsedUrl['path']);
$subdomain = $host[0];
$wp_session = WP_Session::get_instance();
if(isset($wp_session['user_subdomain']))
{
?>
<script>alert('<?php echo "Check first session Set ".$wp_session['user_subdomain']; ?>');</script>
<?php
}
else
{
?>
<script>alert('<?php echo "Check first session Not Set "; ?>');</script>
<?php
}
if($wp_session['user_subdomain']=="test"){
?>
<script>alert('<?php echo "session ".$wp_session['user_subdomain']; ?>');</script>
<?php
$red_url=$wp_session['user_subdomain'].".domain.com";
?>
<script>alert('<?php echo "RED ".$red_url; ?>');</script>
<?php
header("location:$red_url");
}
else if($subdomain=="test"){
$red_url=$_SERVER['SERVER_NAME'];
$wp_session['user_subdomain']=$subdomain;
?>
<script>alert('<?php echo "get ".$subdomain; ?>');</script>
<script>alert('<?php echo "check set session ".$wp_session['user_subdomain']; ?>');</script>
<?php
header("location:$red_url");
}
I have added '_SESSION' in wp_unregister_GLOBALS function, I found it somewhere on google.
You have to set session id on subdomain too.
Try to add this line
ini_set('session.cookie_domain', '.example.com' );
More info
We give priority to set session. The following code will works fine.
add_action('init', 'myStartSession', 1);
function myStartSession() {
if(!session_id()) {
session_start();
}
}

Shared variables between php $_GET and $POST

Why page can not get the $temp_kt value? I tested $_SESSION['temp_kt'] and $_ENV['temp_kt'], neither worked.
<?php
$temp_kt=0;
if(isset($_POST['db']))
{
if($_POST['db']=="feedback")
{
global $temp_kt;
$temp_kt=$_POST['temp_kt'];
}
exit();
}
if(isset($_GET['q']))
{
echo "temp_kt=".$temp_kt;
}
?>
You have exit in if(isset($_POST['db'])) which mean that you can't have both if statements. If you want to save that value in session you should use code like this:
<?php
session_start();
if (isset($_POST['db'])) {
if ($_POST['db']=="feedback") {
$_SESSION['temp_kt'] = $_POST['temp_kt'];
}
exit();
}
if (isset($_GET['q'])) {
echo "temp_kt=" . $_SESSION['temp_kt'];
}
?>
session_start function will enable session for you, you need it at the begining when you set and get session values (it will send cookie to the browser - using header - so you can't have any echo before).

Categories