php session can not pass variable to other page - php

I have a formal code, but the session variable can't be passed to another page.
So here is my sample code. However, the result of this code is 0.:
page1 code:
<?php
session_start();
$_SESSION['try'] = 5;
header('Location: page2.php');
?>
page2 code:
<?php
session_start();
$test = $_SESSION['try'];
echo $test;
?>

From the manual:
session_start() creates a session or resumes the current
one based on a session identifier passed via a GET or POST request,
or passed via a cookie.
pay attention to the last two lines of code:
<?php
// page1.php
session_start();
echo 'Welcome to page #1';
$_SESSION['favcolor'] = 'green';
$_SESSION['animal'] = 'cat';
$_SESSION['time'] = time();
// Works if session cookie was accepted
echo '<br />page 2';
// Or maybe pass along the session id, if needed
echo '<br />page 2';
?>

Related

PHP Session Variables are not passed to next page

I have 2 PHP pages in the same folder. I am trying to set a session variable on one page (test.php), then read its value on another (test2.php). However, the session variable gets lost on the second page.
test.php
<?php
session_start();
$_SESSION["test"] = "123hello";
$hello = $_SESSION["test"];
echo "out: $hello";
$sid = session_id();
echo "<br> sid: $sid";
?>
test2.php
<?php
session_start();
$hello = "test";
if(isset($_SESSION["test"]))
{
$hello = $_SESSION["test"];
}
echo "out: $hello";
$sid = session_id();
echo "<br> sid: $sid";
?>
First I visit test.php, which outputs:
out: 123hello
Then I visit test2.php, which outputs:
out: test
Which means in test.php, the session variable does keep its value. However, for some reason it gets lost in test2.php.

How to pass two value from one page to another in php, passing using session

How to pass two value from one page to another in PHP using session.
$account=$_SESSION["account_no"];
$account1=$_SESSION["account_no"];
Session will be available through out the application (in all pages) until you destroy it.
To set a session,
<?php
session_start();
$_SESSION['variable_name_1'] = "value_1"; // or $_POST['accountno_1'];
$_SESSION['variable_name_2'] = "value_2"; // or $_POST['accountno_2'];
?>
In the other page, to get the values
<?php
session_start();
echo $_SESSION['variable_name_1'];
echo $_SESSION['variable_name_2'];
?>
FILE-1: WHERE YOU NEED TO SAVE THE ACCOUNT TO SESSION
<?php // NOTICE THAT THERE IS NO SPACE BEFORE <?php [THIS IS IMPORTANT!!!]
// FILE-NAME: file_1.php WHERE YOU HAVE TO SET THE SESSION VARIABLE
//FIRST CHECK IF SESSION EXIST BEFORE STARTING IT:
if (session_status() == PHP_SESSION_NONE || session_id() == '') {
session_start();
}
$_SESSION["account_no"] = $account;
FILE-2: WHERE YOU NEED TO GET THE ACCOUNT FROM SESSION
<?php // NOTICE THAT THERE IS NO SPACE BEFORE <?php [THIS IS IMPORTANT!!!]
// FILE-NAME: file_2.php WHERE YOU NEED TO READ THE SESSION VARIABLE
//FIRST CHECK IF SESSION EXIST BEFORE STARTING IT:
if (session_status() == PHP_SESSION_NONE || session_id() == '') {
session_start();
}
// READ THE ACCOUNT NUMBER FROM SESSION DATA...
$account = $_SESSION["account_no"];
On the first page:
session_start();
$_SESSION['value1'] = 'First value';
$_SESSION['value2'] = 'Second value';
On the second page:
session_start();
$value1 = $_SESSION['value1'];
$value2 = $_SESSION['value2'];
File:1 where data will be store Session
<?php
session_start(); //before HTML tag
$_SESSION['one'] = $account_no1;
$_SESSION['two'] = $account_no2;
?>
File2: where you like to retrieve session
<?php
session_start();
echo $_SESSION['one'];
echo $_SESSION['two'];
?>
Sessions is a global variable in PHP.
Just create two session variables as use anywhere
<?php
session_start(); // should be at top of page or before any output to browser
$_SESSION['account'] = $account;
$_SESSION['account1'] = $account1;
Now access these session variables anywhere in any page but should start session before use, like:
<?php
session_start();
echo $_SESSION['account'];

Webmatrix session_destroy()

I have written a simple code for $_SESSION variable in the first php file:
<?php
session_start();
$_SESSION["name"] = "John";
?>
and in another php file to render this:
<?php
session_start();
echo $_SESSION["name"];
?>
But after that i used session_unset(); and session_destroy(); and after that i can't render any new $_SESSION variable nor the existing one. I am using Microsoft WebMatrix program and Chrome as main browser. Any suggestions? Thank you in advance.
That is because session_destroy(); destroys the current session and also sends a header to the browser to delete the session variable. In the same time the session is deleted on the server (in PHP) and the $_SESSION variables can no longer be used. You can always try to save the $_SESSION in another variable;
session_start();
$_SESSION['test'] = 'foo';
Next page:
session_start();
$saveSession = $_SESSION;
session_destroy();
var_dump($_SESSION); //Gives an empty array
var_dump($saveSession); //Still has ['test' => 'foo']
More information: http://php.net/manual/en/function.session-destroy.php and http://php.net/manual/en/book.session.php
Also, sidenote, you do not need to open and close PHP tags if they are combined;
<?php
session_start();
echo $_SESSION["name"];
?>
works just as well as
<?php
session_start();
?>
<?php
echo $_SESSION["name"];
?>

session can not be cleared

Look at the example code directly:
<?php
// page1.php
session_start();
echo 'Welcome to page #1';
$_SESSION['favcolor'] = 'green';
$_SESSION['animal'] = 'cat';
$_SESSION['time'] = time();
echo '<br />page 2';
?>
And another page:
<?php
// page2.php
session_destroy();
session_unset();
session_start();
echo 'Welcome to page #2<br />';
echo $_SESSION['favcolor']; // green
echo $_SESSION['animal']; // cat
echo date('Y m d H:i:s', $_SESSION['time']);
echo '<br />page 1';
?>
Although I call session_destroy(), session_unset(), I still get the data coming from page1. why? and how to really clear the session? Thanks!
This should do the trick
session_start();
$_SESSION = array();
session_unset();
But just for clarity, this is happening to you because you have to call session_start() first
session_start();
session_destroy();
session_unset();
You have to start the session first on the second page page2.php.Put session_start(); at the top on second page.

Need advice about transfering HTTP_REFERER to another page

So I'm currently trying to save page1.php's HTTP_REFFERER as a variable and transfer that variable to another page. I've tried using $_SESSION[] and $_COOKIES[] methods but it didn't work. page2.php shows referrer as page.php.
Any suggestions?
page1.php :
<?php session_start();
$variable1 = $_SERVER['HTTP_REFERER'];
$_SESSION['ref'] = $variable1;
page2.php :
<?php session_start();
$_SESSION['ref'] = $variable1;
echo $variable1; ?>
You inverted the variable and the value in your 2nd page
<?php
session_start();
$variable1 = $_SESSION['ref'];
echo $variable1;
$_SERVER['HTTP_REFERER'] is a predefined env variable. To access stored variable use for example $_SESSION['HTTP_REFERER'] on page2.php
In page1.php, make sure to end the code correctly:
<?php session_start();
$variable1 = $_SERVER['HTTP_REFERER'];
$_SESSION['ref'] = $variable1;
?>
In page2.php just print_r the $_SESSION['ref'] or assign it to a variable and then use echo.
Also the order of the variables is incorrect, should be:
$variable1 = $_SESSION['ref'];
HTH.

Categories