SESSION variable help
Moderator: General Moderators
SESSION variable help
I am having a very difficult time with $_SESSION variables. Probably because I do not understand them that well. I have read a lot of manuals and info about them, but I still do not quite understand, because my code does not work.
I am trying to set a $_SESSION[‘valid_user’] in a function and use it on other php pages on my website. But the session variable is not set once I go to the other page.
Can someone show me in “laymen’s” terms how I can use a session variable from page to page?
I know this is general, but maybe we can start here.
Thank you.
I am trying to set a $_SESSION[‘valid_user’] in a function and use it on other php pages on my website. But the session variable is not set once I go to the other page.
Can someone show me in “laymen’s” terms how I can use a session variable from page to page?
I know this is general, but maybe we can start here.
Thank you.
- AbraCadaver
- DevNet Master
- Posts: 2572
- Joined: Mon Feb 24, 2003 10:12 am
- Location: The Republic of Texas
- Contact:
Re: SESSION variable help
This is the simplest example. Load file1.php and click the 'Click me' link that goes to file2.php and echo the var that was set in the previous page.
file1.php
file2.php
file1.php
Code: Select all
<?php
session_start();
$_SESSION['test'] = "Hi";
?>
<a href="file2.php">Click me</a>Code: Select all
<?php
session_start();
echo $_SESSION['test'];
?>mysql_function(): WARNING: This extension is deprecated as of PHP 5.5.0, and will be removed in the future. Instead, the MySQLi or PDO_MySQLextension should be used. See also MySQL: choosing an API guide and related FAQ for more information.
- SimpleManWeb
- Forum Commoner
- Posts: 57
- Joined: Wed Dec 30, 2009 4:15 pm
- Location: New Hampshire, USA
Re: SESSION variable help
AbraCadaver's example is a great place to start. I just wanted to emphasize that the "session_start()" function is very important. It's what tells your second PHP file to use the batch of session variables that has already been started. Without calling that function, your second PHP file will simply use it's own batch of Session variables, which will be blank. That's probably the issue you were having.
-
ahmedfaraz
- Forum Newbie
- Posts: 2
- Joined: Mon Jan 11, 2010 6:40 am
Re: SESSION variable help
Hi,
I am having problem with Session variables. I have a quotation form on my site. After posting the form inputs, I write the input values in session variables and finally I post the form to payment gateway. Upon returning from Payment gateway, I get some variables from payment gateway like order id , transaction status etc, But now My PHP page did not get those session variables which I store before going to payment gateway. This is not happening every time but out of 10, 1 transaction is infected. So based upon the transaction status I generate some pdf files and email to the client. As I did not get those session variables so unable to create PDF documents
I am running my site on Windows with IIS 6 with PHP 5.2.6 as a CGI.
Any help ??
I am having problem with Session variables. I have a quotation form on my site. After posting the form inputs, I write the input values in session variables and finally I post the form to payment gateway. Upon returning from Payment gateway, I get some variables from payment gateway like order id , transaction status etc, But now My PHP page did not get those session variables which I store before going to payment gateway. This is not happening every time but out of 10, 1 transaction is infected. So based upon the transaction status I generate some pdf files and email to the client. As I did not get those session variables so unable to create PDF documents
I am running my site on Windows with IIS 6 with PHP 5.2.6 as a CGI.
Any help ??
Re: SESSION variable help
Thanks for the suggestions!
I created the file1.php and file2.php. After running file1.php and clicking the link, nothing showed up from running file2.php. Session variable was blank. So I modified file2.php so I could get some output.
Here is my new file2.php:
1. <?php
2. session_start();
3. $tr=$_SESSION['test'];
4. echo "variable test is $tr";
5. ?>
6. <a href="file2.php">Click me, too</a>
When file2.php runs now I get output of:
1. variable test is [blank] Click me, too
My $tr variable is blank. I can't see the session variable set in file1.php.
My session variables are not getting stored properly. Any other thoughts?
Steve
I created the file1.php and file2.php. After running file1.php and clicking the link, nothing showed up from running file2.php. Session variable was blank. So I modified file2.php so I could get some output.
Here is my new file2.php:
1. <?php
2. session_start();
3. $tr=$_SESSION['test'];
4. echo "variable test is $tr";
5. ?>
6. <a href="file2.php">Click me, too</a>
When file2.php runs now I get output of:
1. variable test is [blank] Click me, too
My $tr variable is blank. I can't see the session variable set in file1.php.
My session variables are not getting stored properly. Any other thoughts?
Steve
- Jonah Bron
- DevNet Master
- Posts: 2764
- Joined: Thu Mar 15, 2007 6:28 pm
- Location: Redding, California
Re: SESSION variable help
I ran phpinfo() and it had a lot of info. I didn't see anything relating to session vars, but I may have missed it.
I run on iPower hosted web site, so I don't have control of php.ini.
I run on iPower hosted web site, so I don't have control of php.ini.
- AbraCadaver
- DevNet Master
- Posts: 2572
- Joined: Mon Feb 24, 2003 10:12 am
- Location: The Republic of Texas
- Contact:
Re: SESSION variable help
Put this at the top of both files:
I think PHP can be compiled with or without session handling. Check phpinfo() and make sure you see something like this:
session
Session Support enabled
Registered save handlers files user
Registered serializer handlers php php_binary wddx
Code: Select all
error_reporting(E_ALL);
ini_set('display_errors', '1');session
Session Support enabled
Registered save handlers files user
Registered serializer handlers php php_binary wddx
mysql_function(): WARNING: This extension is deprecated as of PHP 5.5.0, and will be removed in the future. Instead, the MySQLi or PDO_MySQLextension should be used. See also MySQL: choosing an API guide and related FAQ for more information.
Re: SESSION variable help
Well – that got some response!
I added the two lines of code after the session_start().
From file1.php this is what we got:
Warning: Unknown(): open(/var/php_sessions/sess_b1b2372f1f3dddebb639999927f0daeb, O_RDWR) failed: No such file or directory (2) in Unknown on line 0
Warning: Unknown(): Failed to write session data (files). Please verify that the current setting of session.save_path is correct (/var/php_sessions) in Unknown on line 0
From file2.php we got:
Notice: Undefined index: test in /hermes/bosweb/web208/b2088/ipw.bringing/public_html/public_html_ugodtext/file2.php on line 5
variable test is Click me, too
Warning: Unknown(): open(/var/php_sessions/sess_b1b2372f1f3dddebb639999927f0daeb, O_RDWR) failed: No such file or directory (2) in Unknown on line 0
Warning: Unknown(): Failed to write session data (files). Please verify that the current setting of session.save_path is correct (/var/php_sessions) in Unknown on line 0
It looks like it is trying to write session files in /var/php_sessions directory. Can we change where the session path is to be written so I can control permissions?
Steve
I added the two lines of code after the session_start().
From file1.php this is what we got:
Warning: Unknown(): open(/var/php_sessions/sess_b1b2372f1f3dddebb639999927f0daeb, O_RDWR) failed: No such file or directory (2) in Unknown on line 0
Warning: Unknown(): Failed to write session data (files). Please verify that the current setting of session.save_path is correct (/var/php_sessions) in Unknown on line 0
From file2.php we got:
Notice: Undefined index: test in /hermes/bosweb/web208/b2088/ipw.bringing/public_html/public_html_ugodtext/file2.php on line 5
variable test is Click me, too
Warning: Unknown(): open(/var/php_sessions/sess_b1b2372f1f3dddebb639999927f0daeb, O_RDWR) failed: No such file or directory (2) in Unknown on line 0
Warning: Unknown(): Failed to write session data (files). Please verify that the current setting of session.save_path is correct (/var/php_sessions) in Unknown on line 0
It looks like it is trying to write session files in /var/php_sessions directory. Can we change where the session path is to be written so I can control permissions?
Steve
Re: SESSION variable help
from phpinfo()
Session Support enabled
Registered save handlers files user
Directive Local Value Master Value
session.auto_start Off Off
session.bug_compat_42 Off Off
session.bug_compat_warn On On
session.cache_expire 180 180
session.cache_limiter nocache nocache
session.cookie_domain no value no value
session.cookie_lifetime 0 0
session.cookie_path / /
session.cookie_secure Off Off
session.entropy_file no value no value
session.entropy_length 0 0
session.gc_divisor 1000 1000
session.gc_maxlifetime 1440 1440
session.gc_probability 1 1
session.name PHPSESSID PHPSESSID
session.referer_check no value no value
session.save_handler files files
session.save_path /var/php_sessions /var/php_sessions
session.serialize_handler php php
session.use_cookies On On
session.use_only_cookies Off Off
session.use_trans_sid On On
Session Support enabled
Registered save handlers files user
Directive Local Value Master Value
session.auto_start Off Off
session.bug_compat_42 Off Off
session.bug_compat_warn On On
session.cache_expire 180 180
session.cache_limiter nocache nocache
session.cookie_domain no value no value
session.cookie_lifetime 0 0
session.cookie_path / /
session.cookie_secure Off Off
session.entropy_file no value no value
session.entropy_length 0 0
session.gc_divisor 1000 1000
session.gc_maxlifetime 1440 1440
session.gc_probability 1 1
session.name PHPSESSID PHPSESSID
session.referer_check no value no value
session.save_handler files files
session.save_path /var/php_sessions /var/php_sessions
session.serialize_handler php php
session.use_cookies On On
session.use_only_cookies Off Off
session.use_trans_sid On On
- AbraCadaver
- DevNet Master
- Posts: 2572
- Joined: Mon Feb 24, 2003 10:12 am
- Location: The Republic of Texas
- Contact:
Re: SESSION variable help
My guess is that /var/php_sessions doesn't exist. I've never had to monkey with this, but try editing php.ini and changing:
Code: Select all
session.save_path = /tmpmysql_function(): WARNING: This extension is deprecated as of PHP 5.5.0, and will be removed in the future. Instead, the MySQLi or PDO_MySQLextension should be used. See also MySQL: choosing an API guide and related FAQ for more information.
Re: SESSION variable help
Shawn -
Thanks for all your help!
I do not have access to php.ini for I am hosted by iPower and on a virtual server shared by others.
Is there a way to set the sessions path inside my php to use my own directory? I have looked at manuals and it looks like I can, but I haven't seen a good example how to put it into my code.
Steve
Thanks for all your help!
I do not have access to php.ini for I am hosted by iPower and on a virtual server shared by others.
Is there a way to set the sessions path inside my php to use my own directory? I have looked at manuals and it looks like I can, but I haven't seen a good example how to put it into my code.
Steve
Re: SESSION variable help
Thank you for your help.
I put session_save_path("/my/dir/tmp/") before session_start() in my php files and that seems to work!
I will do more testing with my actual code to make sure, but I hope this takes care of my problems.
Again, thank you!
Steve
I put session_save_path("/my/dir/tmp/") before session_start() in my php files and that seems to work!
I will do more testing with my actual code to make sure, but I hope this takes care of my problems.
Again, thank you!
Steve