Pass $_SESSION variables to included file - php

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

Related

Session ID from class into an other php file

I've got a class, in which i start a Session.
Now I would need the Session ID from this Session in an other file - my header.php file.
Im quite new to PHP, so I would appreciate any help.
Thanks a lot Rohan
If you wish to use the Session in other files, you can simply use it using the $_SESSION variable.
A simple example::
test1.php
<?php
session_start();
$_SESSION['Test_Value'] = "100"; //set session variable.
?>
test2.php
<?php
session_start();
echo $_SESSION["Test_Value"]; //get session variable.
?>
Now when you open test1.php and then open test2.php you will see output of 100 which was set from test1.php

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

Why is my session failing to work?

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.

How to pass variables between php scripts?

Is there any way to pass values and variables between php scripts?
Formally, I tried to code a login page and when user enter wrong input first another script will check the input and if it is wrong, site returns to the last script page and show a warning like "It is wrong input". For this aim, I need to pass values from scripts I guess.
Regards... :P
To pass info via GET:
header('Location: otherScript.php?var1=val1&var2=val2');
Session:
// first script
session_start();
$_SESSION['varName'] = 'varVal';
header('Location: second_script.php'); // go to other
// second script
session_start();
$myVar = $_SESSION['varName'];
Post: Take a look at this.
You should look into session variables. This involves storing data on the server linked to a particular reference number (the "session id") which is then sent by the browser on each request (generally as a cookie). The server can see that the same user is accessing the page, and it sets the $_SESSION superglobal to reflect this.
For instance:
a.php
session_start(); // must be called before data is sent
$_SESSION['error_msg'] = 'Invalid input';
// redirect to b.php
b.php
<?php
session_start();
echo $_SESSION['error_msg']; // outputs "Invalid input"
Can't you include (or include_once or require) the other script?
The quick way would be to use either global or session variables.
global $variable = 'something';
The 'better' way of doing it would be to include the script and pass the variable by parameter like
// script1.php contains function 'add3'
function add3( $value ) {
return $value + 3;
}
// script2.php
include "script1.php";
echo 'Value is '.add3(2); // Value is 5
I would say that you could also store a variable in cache if you really need.
You can use:
temporary file (e.g. tempnam()),
cache (NoSQL: memcached, redis),
session variable ($_SESSION), but you need to start the session first.
I use extract() method to pass variable among PHP Scripts. It look like below example:
1. File index.php
<?php
$data = [
'title'=>'hello',
'content'=>'hello world'
];
extract($data);
require 'content.php';
2. File content.php :
<?php
echo $title;
echo $content;

Php session problem

I m working on site which does not allow me to initialize session i-e whenever i write session_start(); the page does not load ?????
could you perhaps post a part of your code?
Also, session_start() has to be called before you send anything back to the user. Which normally means it should be on first line of your code.
<?php session_start(); ?>
Let's see what does the $_SESSION array store after we uncomment session_start() line:
<?php
error_reporting(-1); // Will report everything, comment out when not needed
ini_set('session.use_trans_sid', false);
session_start();
var_dump($_SESSION);
Maybe you need to put session_start after __autoload so that objects in $_SESSION are instantiated correctly (ie. not as stdclass.)

Categories