I have a page, index.php, which includes another page, session.php. In session.php, I would like a session to start, and to set a session variable.
Upon running index.php, I would like the session variable set in session.php to be displayed.
I expected my code to work, but I have found that it does not.
index.php:
<?php
include "path/to/session.php";
echo $_SESSION['var'];
?>
session.php:
<?php
session_start();
$_SESSION['var'] = "yes";
?>
I expected the output to be yes, and instead return no output.
EDIT:
I have tried this on Firefox and Chrome, to no discernible difference.
Upon inserting error handling as suggested below, it has not printed any errors.
Putting an echo "hi"; on session.php will output hi on index.php, so there's no obvious issue with the include or the pathing.
If I run session.php, having it echo the session_id(), the output will be the same each time I refresh the page; if I have the index.php echo the session_id(), it will not produce an output, and the output sent from session.php will be different each time I refresh the page, and does not appear to be reflective of session.php's session_id() when run from that page.
You have to include session_start() at the top of the index.php file
<?php
session_start();
include "path/to/session.php";
echo $_SESSION['var'];
?>
Related
I have a doubt regarding php session variable declaration.
I have two php files. 1. head.php 2. body.php
head.php
session_start();
$id = $_Session['id'];
$name = $_Session['name'];
some other text like include js include css etc.
body.php
include 'head.php';
echo $id;
echo $uid;
is this correct? or do i need to add session_start(); in body.php file as well.
TL;DR
You do not need to call the function again after you have called it in your header file.
And Oh, it is $_SESSION , not $_Session
where to declare session_start if multiple files are included?
Before you send any output to the browser. Nothing else matters. There can be hundreds of PHP code lines before that as long as they don't send any output you can call session_start(); after that.
Obviously you will not have SESSION values before that :)
Please elaborate i didn't understand
Don't echo or print etc anything before calling session_start(), not even outside PHP tags. Don't place any html or blank spaces before the PHP tags either. Absolutely nothing should be sent to browser before you call that function.
Wrong usage
<html>
<?php
session_start();
?>
Right Usage
<?php
blahblahblah(); // or nothing
session_start();
echo "<html>";
?>
Also if you are including your head.php in many php files you should check session first and then start session. otherwise you will get error.
if(session_id())
{
// session has been started
}
else
{
// session has NOT been started
session_start();
}
Your current code is ok, session start should be in head.php itself
My session does fails to resume when opening another file
I have 2 pages, test.php and test2.php. Each of my files are listed below.
test.php:
<?php
session_save_path('sessBin');
session_id('mySessionID');
session_start();
$_SESSION[1]="yo";
echo $_SESSION[1];
?>
go to test 2
test2.php:
<?php
session_save_path('sessBin');
session_id('mySessionID');
session_start();
echo $_SESSION[1];
echo "bleh";
?>
The first page returns:
"yo" go to test 2
however, when clicking on the link to go to test2.php, only this returns:
"bleh"
I did some research to find that cookies must be enabled. Well, they are... So what is wrong?
NOTE: (A discovery from afterwards)
I did notice that there were several files(including sess_mySessionID) in sessBin so the session is stored. However, the files are empty and the session does not seem to resume
test1.php
// session_save_path('sessBin');
// session_id('mySessionID');
session_start();
$_SESSION['test'] ="yo";
echo $_SESSION['test'];
go to test 2
test2.php
// session_save_path('sessBin');
// session_id('mySessionID');
session_start();
echo $_SESSION['test'];
echo "bleh";
Worked for me !
session_start needs to be defined at the top of every page, right after the opening php tag, on every page where the session data is expected to exist. You must do this before addressing any other session function or variable.
** Update to Answer Based On Your Comments **
check for white space before the opening php tag in test2.php. Also, enable error reporting. You might be sending something out before the session call that is causing session_start() to fail.
Try this:
config.php
<?php
session_start();
?>
In all your other files at the top add this:
include'config.php';
That way you will always have a file where you can put important stuff you might need on other pages as well, so you don't have to retype it every time.
Why I am getting an empty array of the session when I am trying a var_dump in another page?
In the server, the session is stored without any content, only with the name of the id.
With cookies all works well. Sometimes (yes, sometimes), I restart the server, and then, the sessions works well too. What may cause this issue?
Maybe a bug of the php 5.1.6? or a problem in the config of the server?
index_2.php
<?php
session_start();
$_SESSION['xxx'] = "tessstsse";
var_dump($_SESSION);//here show the correct session
header( 'Location: index_3.php');
index_3.php
<?php
session_start();
var_dump($_SESSION);
The output of this will be:
array
empty
Give this a try.. if this works I'd re examine your code else it is probably an issue with your server...
page1.php
<?php
session_start();
$_SESSION['auth'] = "true";
$_SESSION['superhero'] = "batman";
?>
Click here
page2.php
<?php
session_start(); // start the session before using it
//echo $_SESSION['auth']; // will output 'true'
print_r($_SESSION);
?>
destroy.php
<?php
session_start(); // start the session before using it
session_destroy(); // deletes the current session..
?>
I have the web server without space to store the session content.
0% of free space. Delete some data and the problem is solved.
I'm writing a user login system, and I (like so many others) am having a problem with my sessions.
Here's the pointer from the login script when the inputs are validated:
session_start();
$_SESSION['id']=$id;
header('location: memberhome.php');
Here's the first thing on memberhome.php:
<?php
session_start();
$id=$_SESSION['id'];
?>
And later in memberhome.php:
You are logged in as: <?php echo $id; ?>
The problem is $_SESSION['id'] is apparently empty so the echo $id prints nothing.
An alternate that also does NOT work:
//removed session_start and $_SESSION bit from the top
You are logged in as: <?php session_start(); echo $_SESSION['id']; ?>
NOW, here's the weird part. This method DOES work:
You are logged in as: <?php echo session_start();$_SESSION['id']; ?>
You can see the session_start() is moved AFTER the echo. This works when the page loads from the login script. However, upon refresh, it does NOT work once again.
I've tried a bunch of alternatives and spent a few hours searching for answers in previous questions. I also looked at my phpinfo() for something fishy and found nothing. This is entirely what my progress is hinging on. Thanks!
First of all, please enable debugging:
error_reporting(E_ALL);
ini_set('display_errors', '1');
Second, session_start() needs to be at the top of the page. So the line you wrote;
You are logged in as: <?php echo session_start();$_SESSION['id']; ?>
will never work.
The following line needs to be on top of the page, before any HTML etc.
<?php
session_start();
$id=$_SESSION['id'];
?>
Have you tried:
print_r($_SESSION);
to examine the contents of the session?
Make sure you're calling session_start() before you output anything on the page. The standard cookie-based sessions require some header information to be exchanged, which must be done before you send any content.
You're most likely running into output buffering, which is why it sometimes works and other times it does not. Generally speaking, stick to starting the session before any output is generated, you'll find your code works better.
use
ob_start(); #session_start();
on the top of the both page
index.php
<?php
session_start();
header("Location: somewhere.php");
?>
<html>
<head></head>
<body>
<?php $_SESSION['foo'] = 'bar'; ?>
</body>
</html>
somewhere.php
<?php
session_start();
echo $_SESSION['foo'];
?>
I set a session variable in the body after a header call in index.php. Then it's found in somewhere.php. This happens even after restarting the browser. How is this happening?
Well, why not?
// starts session, sets cookie with session id
session_start();
// outputs Location header
header("Location: somewhere.php");
// rest of code keeps executing!
// sets session value foo
$_SESSION['foo'] = 'bar';
Just because you're outputting a Location header doesn't mean the rest of the script doesn't execute.
The session value is set and saved on the server, this is completely independent of whether headers have already been sent or not. The only header that needs to be send to the client is a cookie containing the session id, this can happen before or after populating the session values in the server's memory.
EDIT: Erhm. I'm tired. I misunderstood your question. Feel free to ignore this post
Session variables are superglobals. A cookie gets set in the client's browser with a session id. Whatever you set in $_SESSION get's stored on the server linked to the client's session id. When the user browses to a web page, PHP automatically populates $_SESSION with any previous data, until the session has expired.
try this and see if you are getting the same results:
<?php
session_start();
unset($_SESSION['foo']);
header("Location: somewhere.php");
?>
Edit:
<?php
session_start();
unset($_SESSION['foo']);
header("Location: somewhere.php");
exit;//maybe this will stop the script from setting that session
?>
Becuase when you compiler rached on 4th line "header("Location: somewhere.php");"
your control goes to somewhere.php
You can use this.
index.php
< ?php
session_start();
$_SESSION["foo"] = "bar";
header("Location: somewhere.php");
?>
Surely It will run