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
Related
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.
I am trying to process simple HTML form when on submit page1.php is called. page1.php internally calls page2.php
page1.php looks like this:
<?php
session_start();
$sesid = session_id();
$_SESSION['cname'] = #trim(stripslashes($_POST['companyname']));
$_SESSION['fname'] = #trim(stripslashes($_POST['firstname']));
$_SESSION['lname'] = #trim(stripslashes($_POST['lastname']));
$_SESSION['eaddr'] = #trim(stripslashes($_POST['eaddress']));
echo " $_SESSION[cname]" ;
echo " $_SESSION[otprdsvc]";
include_once $_SERVER['DOCUMENT_ROOT'] .'/appconnector.php';
?>
Page2.php looks like this:
<?php
echo "$_SESSION[cname]";
?>
I do get output of echo $_SESSION[cname]; on first page. However, data is not saved to disk when I check session cookie.
Here is data from session cookie on disk:
cname|s:0:"";fname|s:0:"";lname|s:0:"";eaddr|s:0:"";webaddr|N;cmsg|s:0:"";drpopt|N;otprdsvc|s:0:"";securimage_code_disp|a:1:{s:7:"default";s:6:"n55Zmr";}securimage_code_value|a:1:{s:7:"default";s:6:"n55zmr";}securimage_code_ctime|a:1:{s:7:"default";i:1398221627;}
I don't understand why cname is coming as "" ( no data / null ) where in fact I am getting response.
Little bit of history what I have done till now
session.save_path was not enabled earlier in php.ini file so I enabled it.
I was using autocomplete = off in html form earlier so I removed it.
securimage is packaged solution I am using for captcha which is saving cookie data but I don't know+understand anything what it did internally.
Can you please suggest what needs to be done in order to have session cookie data:
saved & available on disk in file
make it available when page2 is processed.
Thank you in advance and I apprecriate your time and comments.
You will have to always do session_start(); on every page before using the $_SESSION variable.
So page2 will be
<?php
session_start();
echo "$_SESSION[cname]" ;
?>
Try using this:
For page1.php
....
echo $_SESSION['cname'];
echo $_SESSION['otprdsvc'];
....
For page2.php
<?php
session_start();
echo $_SESSION['cname'];
?>
You have to use session_start() on every page wherever you want to call the session data.
I have solved this problem. Page2.php was submitting itself so session data was getting lost. I now have modified flow of data processing. I also used session.write_close() after writing data to session variables on page1.php so that also helped.
Thank you for your comments.
I have two pages of php on the same folder , Im wondering what can I do to use that variable in another page like page2.php
page1.php
$variable = "Variable";
page2.php
//get $variable value from page1.php here
Any suggestions? Thank You
Use Sessions.
On page1.php:
session_start();
$_SESSION['var1'] = 'some text';
On Page2.php:
if(!isset($_SESSION)){
session_start();
}
echo $_SESSION['var1'];
You will get some test as output.
Use session variables.
Just write session_start() at the beginning of files to start a session and assign the session variables.
eg:
File-1
session_start();
$_SESSION['var'] = "var";
File-2
session_start();
if(isset($_SESSION['var']))
echo $_SESSION['var']; // will print "var"
You can use this throughout till the session destroyed. To destroy the session (may be on logout)-
if(isset($_SESSION['var']))
unset($_SESSION['var']);
To completely destroy the session-
session_destroy();
Or you can try constants like so:
constants.php
<?php
define("VARIABLE_1", "Hotdog");
?>
page2.php
<?php
include_once("constants.php");
echo VARIABLE_1;
// output == Hotdog
?>
Just include the constants php script in the pages you want to use the variables in.
Check PHP Sessions:
page1.php
session_start();
$_SESSION['variable'] = "Variable";
page2.php
session_start();
echo $_SESSION['variable']
You can require_once("page2.php") in page1.php
That will allow you to use variables in page 2 across page 1.
Alternatively, you can also use sessions.
session_start() starts a new session or resumes an existing session. Include this on the first line of both pages.
Session parameters are set using $_SESSION["parameter-name"] = "something";
You can use the variables anywhere in the script.
To terminate sessions, use session_destroy()
You can use Sessions to achieve this , also you should know about QueryStrings.
For example:
Navigate to another page say "location2.php?Variable=123" here ?Variable is your var and 123 is value.
And on location2.php you can get this by simply $_GET["Variable"];
This can be achieved in many ways. You can use php session, you can include a file where you create that variable, or you can define it as a consant.
All of that depends how complex is that problem and which suits you best in this case.
I have problem with session. I create a different Session file with name Session.php & want to include this file in each page of website to manage session. can it work? suggest me please I am a fresher and working first time woth session .
my session.php file is as follow
<?php include("lib/config.php");
function start_session() {
$_SESSION['userid']=$rows[0];
$_SESSION['first_name']=$rows[2];
$_SESSION['last_name']=$rows[3];
$_SESSION['user_email']=$rows[4];
$_SESSION['user_password']=$rows[5];
$_SESSION['user_gen']=$rows[6];
$_SESSION['user_contact']=$rows[7];
$_SESSION['picture']="profile/".$rows_pic[0];
$user_id=$_SESSION['userid'];
$fnam=$_SESSION['first_name'];
$lnam=$_SESSION['last_name'];
$email=$_SESSION['user_email'];
$pic_ed=$_SESSION['picture'];
}
function destroy() {
session_destroy();
}
?>
You need to include session_start() at the top of your file then your function will surely work.
1) You have used $rows in function, what is that variable?
2) yes, you can include that file in all pages, but better you create session.php under lib folder and include that file in lib/config.php. So you just need to include config.php in all pages.
3) You need session_start() at very top most code of your php file.
What is the use of creating session.php file?
You can access session like that:
#session_start();
if you want to write new session than use like that $_SESSION['MySession'] = 'XYZ';
Accessing Session echo $_SESSION['MySession'];
You can access session $_SESSION['MySession'] on every page but you need to include #session_start() at the top of every php file.
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'];