Question:
PhP sessions not working?
A
2009-12-06 01:20:46 UTC
Page 1 is a login page. It logs in fine. I've double-checked, username, view, and logged_in are being set. It forwards to page 2. Page 2 doesn't recognize the cookie. If I use session_start(), page 2 will make a new cookie, not resume the old one.

If I send the session ID from page 1 to page 2 via $_GET (Assignment of POST doesn't work seem to transfer it), and session_id($_GET['session_id']);, it changes the value of session_id() fine, but still seems unable to access the variables within the session.

//PAGE 1
session_start();
$_SESSION['username'] = $username;
$_SESSION['view'] = "all";
$_SESSION['logged_in'] = true;
setcookie("PHPSESSID", "", time() + (15*60), "/");
header("Location: page2.php");

//PAGE 2
//session_start();
session_id($_GET['session_id']);
echo "\n[", session_id(), "]";
Three answers:
?
2009-12-06 02:21:40 UTC
Well your first step is to remove the cookie. You're confusing the server. Cookies and sessions are the same thing, but cookies are client side, whereas sessions are server side (sessions increase security and compatibility so kudos for using them).



The session will remain active until the browser window is closed, so as long as you set your session variables in your login script, you'll have access to them on any page you call the session. The way I authenticate with sessions is to register a unique variable like "$_SESSION['myvariable']" and just check to make sure that unique variable is registered for each secure page. If it is, it means that IP had a successful login and the content will be displayed.



Also, make sure you don't comment out the session call on page 2.
oracle
2009-12-06 02:25:30 UTC
If you are getting a permission denied error, check your configuration (php.ini) for the location of the session cookies and check the filesystem permissions on the location.



Change the filesystem permissions and you should be good to go



HTH
lashon
2016-10-15 04:54:52 UTC
while you're working in the comparable internet site it may be superb. You create a consultation variable using session_register("name_of_variable"); you examine if a variable has been registered using session_is_registered("username") although if i'm thinking this is a thank you to do it in older variations of Hypertext Preprocessor, now you will likely use $_SESSION['name_of_variable'] = "my value"; now to check in and consider using isset if(isset($_SESSION['name_of_variable']... //do something }


This content was originally posted on Y! Answers, a Q&A website that shut down in 2021.
Loading...