Why is my session failing to work? - php

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.

Related

Passing session variables from an included session

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

PHP Session Variables not Giving a Value

Found the solution. Solution at the bottom of the post
I have some code in php using sessions (I'm just testing them out - I want to use them in a login system).
test1.php:
<?php
session_start();
$_SESSION["test"] = "works";
echo $_SESSION["test"];
?>
test2.php:
<?php
echo $_SESSION["test"];
?>
test1.php output the correct value (where I wrote echo $_SESSION["test"];), however when I switch to test2.php, there's nothing. I have checked the cookies (both websites have the same session cookie). Could the problem be a server error?
Found the solution. A simple error like that can create a big problem. At the time, I did not realize that I had to have a session_start() at the beginning of every php webpage that I used session variables in.
There must be a sesssion_start(); at the beginning of EVERY php webpage that you are using session variables in

where to declare session_start if multiple files are included?

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

PHP Sessions Variables & Authentication

I have two pages which I need to pass session information between them. Here's page one.
<?php
session_start();
echo session_id()."<br>";
echo $_SESSION['test']."<br>";
Page two.
<?php
session_start();
echo session_id()."<br>";
$_SESSION['test'] = 'test';
echo $_SESSION['test'];
Page two is in a different directory (same domain), and has Windows Authentication on. Page one is using anonymous authentication. From the output I can see the session ID is the same, but page one doesn't echo the test variable set from page two.
I'm running PHP in IIS 8.5 on Server 2012 R2.
Any help is appreciated.
To clarify, I am calling page two first, and page one will not show the variable.
You need to initialize the variable with a value first before using it.
First run page2.php so, that value of $_SESSION['test'] is set to test
using $_SESSION['test'] = 'test';
Now, run page1.php there you can see the value of $_SESSION['test']
using echo $_SESSION['test']."<br>";
you can also follow these steps to make use of session in a simple way so that you don't have to do session_start() again and again
1. config.php having session_start();
2. page1 having include_once('config.php');
echo session_id()."<br>";
echo $_SESSION['test']."<br>";
3. page2.php having include_once('config.php');
echo session_id()."<br>";
$_SESSION['test'] = 'test';
echo $_SESSION['test'];
Try closing your session in the first page, it's possible that your session file is still open when the second page is called, so you're not seeing the data. Try adding session_write_close() to the first page called.
http://php.net/manual/en/function.session-write-close.php
The issue was with permissions on the session save path. I changed the path to where both directories could write and it works.

$_SESSION values not holding!

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

Categories