PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!
I am trying to learn how to use sessions since I'm working on a log-in based website. In my attempt to learn I've typed a sample from a PHP book (with my own addition of finding and displaying the session_id). It is very basic. A button is pushed which takes the user to page2. Page2 is supposed to show the value of $_SESSION['session_var'] but it doesn't.
Obviously, the problem is that session_var should = "testing" but nothing is displayed because it appears to be empty. The session_id's are the same when echoed on page1 and page2 so that's good.
Not knowing anything about sessions or how they worked before I started doing this I've realized that there might be several reasons for this to not be working as I want it to. If someone is willing to stick it out till the end to help me fix this I'd greatly appreciate it but also some suggestions that might put me in the right direction would also be very useful and appreciated as well.
Here's what I got when I ran the diagnostic you suggested.
Would've gotten back to you sooner about this but apparently someone decided to steal my bike today so I had to make a little trip down to the police station.
Also, I added the two lines of code that you suggested to page1.php and page2.php and got some "Warnings" on both pages. Here is what page1.php and page2.php now look like. The Warnings named some of my website's directories so I just replaced that text with ...filepath....
page1.php with Warnings
Warning: session_start() [function.session-start]: open(/var/php_sessions/sess_bbd4e75608c3f774037aa692fe0186b9, O_RDWR) failed: No such file or directory (2) in /hermes/bosweb/web198/b1980/...filepath.../page1.php on line 4
Warning: session_start() [function.session-start]: Cannot send session cache limiter - headers already sent (output started at /hermes/bosweb/web198/b1980/...filepath.../page1.php:4) in /hermes/bosweb/web198/b1980/...filepath.../page1.php on line 4
Session Array
(
[unique_4a46fa60dc140] => 1
)
This is a test of the sessions feature.
Warning: Unknown(): open(/var/php_sessions/sess_bbd4e75608c3f774037aa692fe0186b9, 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
page2.php
Warning: session_start() [function.session-start]: open(/var/php_sessions/sess_bbd4e75608c3f774037aa692fe0186b9, O_RDWR) failed: No such file or directory (2) in /hermes/bosweb/web198/b1980/...filepath.../page2.php on line 5
Warning: session_start() [function.session-start]: Cannot send session cache limiter - headers already sent (output started at /hermes/bosweb/web198/b1980/...filepath.../page2.php:5) in /hermes/bosweb/web198/b1980/...filepath.../page2.php on line 5
Notice: Undefined index: session_var in /hermes/bosweb/web198/b1980/...filepath.../page2.php on line 11
session_var =
form_var = testing_btn
Warning: Unknown(): open(/var/php_sessions/sess_bbd4e75608c3f774037aa692fe0186b9, 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
Warning: session_start() [function.session-start]: open(/var/php_sessions/sess_bbd4e75608c3f774037aa692fe0186b9, O_RDWR) failed: No such file or directory (2) in /hermes/bosweb/web198/b1980/...filepath.../page1.php on line 4
if you are on a dedicated box,you may want to change the permissions on the directory, adding write permissions. run ls -al to see who the owner of the directory is. allow writing by the user that apache is running under. If it isnt a dedicated box...I am not really sure what to do.
suedoughnyhm wrote:Would've gotten back to you sooner about this but apparently someone decided to steal my bike today so I had to make a little trip down to the police station.
Did you go to file a report or did the police find your bike? If it's still missing, I'm sorry to hear that. I hope you get it back.
For your PHP problem, try this; but don't use it as a permanent solution if it works. I'm pretty sure, like a94060 says, it is a problem with permissions. Hopefully your current working directory has the proper permissions.
Ding ding ding ding!
I've got it working now. It wasn't a permissions problem. I realized I was going to probably have to resort to talking to my host's support so I decided to do a little browsing around on my host's site and found a PHP FAQ section that dealt specifically with sessions.
McInfo, you nailed it. The FAQ defined the path for session_save_path(); and I put that on the line before session_start(); and everything's working as expected now. Thanks a ton for the help. I learned a lot from it. A happy ending to my otherwise crappy weekend which included a stolen bike and the US losing to Brazil in the Confederations Cup Final after being up 2-0 at the half (soccer).
And yes, I went to the police to file a report. You know what the worst part about it was? I bought the bike on Tuesday! Some mother effer's riding around on my brand new bike! Oh well, guess I'll just have to buy a new one with a better lock next time around.