PHP Session Variables are not passed to next page - php

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.

Related

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"];
?>

global a $_SESSION variable

I'm trying to make a login session on PHP, but it appears that the $_SESSION['username'] dies inside the IF sentence (I thought $_SESSION where globals by default), and I cant echo it out of the IF
heres My code
if($name=="admin" && $password=="admin")
{
session_start();
$_SESSION['username'],$_SESSION['sesion'];
$_SESSION['username']=$name;
$_SESSION['sesion']=1;
echo $_SESSION['username'];
echo "<br>";
echo $_SESSION['sesion'];
}
echo "<br>";
echo $_SESSION['username'];
The last echo doesnt print its VALUE, So when I redirect it to another page, the page doesnt take the username value
I'm kind of new in this matter
So dont be so harsh on me :P
How can I do this??
Move session_start() to the top of the file:
// foo.php
<?php
session_start();
//....
if($name=="admin" && $password=="admin")
{
// $_SESSION['username'],$_SESSION['sesion']; // Remove this line
$_SESSION['username']=$name;
$_SESSION['sesion']=1;
echo $_SESSION['username'];
echo "<br>";
echo $_SESSION['sesion'];
}
echo "<br>";
echo $_SESSION['username'];
When You are using sessions in php You must start it with session_start() in every file/page You want to get or set session values, so just add this line to this file and that file You're redirecting to.
session_start() should be above the if statment. best to put it right below the ?php

php session can not pass variable to other page

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';
?>

Help me with Php session vs Header redirect?

I have the following pages:
page1.php
<?php
if (isset($_GET['link'])) {
session_start();
$_session['myvariable'] = 'Hello World';
header('Location: http://' . $_SERVER['SERVER_NAME'] . dirname($_SERVER['REQUEST_URI']) . '/page2.php');
exit;
}
?>
Click Here
page2.php
<?php
print 'Here is page two, and my session variable: ';
session_start();
print $_session['myvariable']; //This line could not output.
exit;
?>
When I try output $_session['myvariable'] I did not get the result hello world message.
I could not find out the solution to fix it?
$_SESSION not $_session. Uppercase.
error_reporting(E_ALL); at the top of the script always helps in such case
session_start() has to be called before you send any output as it relies upon cookies to store the ID.
Also $_SESSION is uppercase
<?php
session_start();
echo $_SESSION['myvariable'];
echo 'Here is page two, and my session variable: ';
exit;
?>
HTTP headers must be the very first output, so session_start() must be at the top of your code.
Other notes:
* $_SESSION should be uppercase.
* Echo > print

Categories