Page 1 of 1

SESSION variable help

Posted: Mon Jan 18, 2010 12:40 pm
by dsmman
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.

Re: SESSION variable help

Posted: Mon Jan 18, 2010 12:47 pm
by AbraCadaver
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

Code: Select all

<?php
session_start();
$_SESSION['test'] = "Hi";
?>
<a href="file2.php">Click me</a>
file2.php

Code: Select all

<?php
session_start();
echo $_SESSION['test'];
?>

Re: SESSION variable help

Posted: Mon Jan 18, 2010 6:02 pm
by SimpleManWeb
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.

Re: SESSION variable help

Posted: Tue Jan 19, 2010 1:09 am
by ahmedfaraz
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 ??

Re: SESSION variable help

Posted: Wed Jan 20, 2010 12:36 pm
by dsmman
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

Posted: Wed Jan 20, 2010 12:55 pm
by Jonah Bron
Is there a setting to enable sessions in php.ini? I don't know. Maybe phpinfo() can tell you something.

Re: SESSION variable help

Posted: Wed Jan 20, 2010 1:11 pm
by dsmman
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.

Re: SESSION variable help

Posted: Wed Jan 20, 2010 1:15 pm
by AbraCadaver
Put this at the top of both files:

Code: Select all

error_reporting(E_ALL);
ini_set('display_errors', '1');
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

Re: SESSION variable help

Posted: Wed Jan 20, 2010 4:25 pm
by dsmman
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

Re: SESSION variable help

Posted: Wed Jan 20, 2010 4:34 pm
by dsmman
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

Re: SESSION variable help

Posted: Wed Jan 20, 2010 6:00 pm
by AbraCadaver
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 = /tmp

Re: SESSION variable help

Posted: Thu Jan 21, 2010 9:55 am
by dsmman
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

Re: SESSION variable help

Posted: Thu Jan 21, 2010 10:12 am
by dsmman
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