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
Related
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'];
?>
I have a single page application based on an index.php page where I start the session and display first contents. Then I load via Ajax contents in different divs based on the links that are clicked.
If I want to access the $_SESSION array in the loaded page (eg. to display user's name) I need to put session_start also in the page that I load via Ajax.
Everything works fine this way but when I see the log I see that php is throwing an error each time I load one of the pages via Ajax saying that
"session already started. Ignoring session_start()".
So: on one side I need to put session_start to access the session array but on the other the session_start command is ignored.
From index.php:
<?php
require '../session_handler.inc.php';
require 'global_functions.php';
session_start();
if(isset($_SESSION['id1'])){
$actusr=$_SESSION['id1'];
}
A sample ajax call:
function loadContent(sourceUrl){
$(".container").load(sourceUrl);
}
The php I need on top of the called page:
<?php
require '../../session_handler.inc.php';
require '../global_functions.php';
session_start();
If I remove the session_start() the $_SESSION will be unavailable
How can I fix this situation? Can I access the session array from the loaded page without starting session?
It's better to use this line instead of session_start().
you might have more than one session_start and using session between those two.
if(session_id() == '') {
session_start();
}
If you are testing on a local WAMP or Xamp environment, I would recommend you clear your cache, close your browser (Not a single tab) and run your app.
Try this 1:
<?php
if(!isset($_SESSION))
{
session_start();
}
?>
Or
2: If you have php>=5.4.0, better use this:
$status = session_status();
if($status == PHP_SESSION_NONE){
//There is no active session
session_start();
}else
if($status == PHP_SESSION_ACTIVE){
//Destroy current and start new one
session_destroy();
session_start();
}
Hope this helps..
move session_start(); to top of your page
<?php
session_start();
require '../session_handler.inc.php';
require 'global_functions.php';
if(isset($_SESSION['id1'])){
$actusr=$_SESSION['id1'];
}
session_handler.inc.php or global_functions.php may already contain session_start() so move the session_start() of index.php to the top. also note that this should be the first statement in index.php
or insert the code below at page top of index.php
<?php
session_start();
?>
and remove the other session_start() in index.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.
i have a first PHP file:
/home/www/subdomain1.domain.tld/file.php
<?php
session_start();
$_SESSION['foo']='bar';
include "/home/www/subdomain2.domain.tld/foo2.php";
?>
and /home/www/subdomain2.domain.tld/foo2.php:
<?php
session_start();
echo $_SESSION['foo'];
?>
The "include" in the first file generates a "500 Internal Server Error", i think it's because session variables are not passed to included files, how can i fix that?
Thank you
Alex
EDIT:
I must use session variables in order to use these variables on every php file on subdomain2.
You don't need to use sessions when including a file. It is all the same request with the same namespace.
file.php:
$foo = 'bar';
include 'foo2.php';
foo2.php
echo $foo; // returns 'bar'
You shouldn't be starting the session in the second file. Since the session was started in file.php, it is already available to foo2.php.
The error may be because PHP output a warning that the session was already started.
For debugging, add error_reporting(E_ALL); ini_set('display_errors', 1); to the beginning of your first PHP script.
You should just be able to do:
file.php
<?php
session_start();
$_SESSION['foo']='bar';
include "/home/www/subdomain2.domain.tld/foo2.php";
foo2.php
i have a first PHP file: /home/www/subdomain1.domain.tld/file.php
and /home/www/subdomain2.domain.tld/foo2.php:
<?php
// session_start(); // remove, do not need this here
echo $_SESSION['foo'];
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