Jump to content

passing session to the next page


lead_zepplin

Recommended Posts

I have my php pages set up like this:

 

1. log in with login.php

 

2. that sends you to main.php

 

3. in main.php, there are links to page-a.php, page-b.php, page-c.php, etc.

 

when you type in a password at login.php, it passes your input to main.php.

 

the correct password is hardcoded in main.php. if it matches, a session variable is set, which should be able to be used on page-a.php, page-b.php, page-c.php, etc. to verify that whoever accesses those pages has gone through the login process. if the session variable doesn't match (or null) the user is redirected to the login page.

 

also the session variable is checked (recursively) when accessing main.php, just like the other pages.

 

the problem is, it's as if each page starts over. the session variable does not make it beyond the page that sets it. it should be passed on to the next page but it's not.

 

I used an echo statement in page-a.php to verify and rem'd out the rest of the code. no echo because session value is null, page goes on to load the html. without the code rem'd out it redirects the user to login.php.

 

code:

 

LOGIN.PHP

<?
session_start();  
if(isset($_SESSION['aaa']))
unset($_SESSION['aaa']); 
?>
<html>
<head>
<title>title</title>
</head>
<body>
<table width="400" align="center" border="0" bordercolor="#000099" bordercolordark="#000066" bordercolorlight="#6666FF">
  <tr bgcolor="#B0C4DE"> 
    <td> 
      <form action="main.php" method="POST">
        <p align="center"><strong><font size="2" face="Verdana, Arial, Helvetica, sans-serif" color="#FFFFFF"><br>
          Password</font></strong> 
          <input name="pwfield" type="password" value size="20" maxlength=20>
<input type="submit" value="Login"></p>
      </form>
    </td></tr></table>
</body></html>

 

MAIN.PHP

<?
if ($_SESSION['aaa']!="abcdef")
{
$password="twinkies";
if ($_POST["pwfield"]==$password)
$_SESSION['aaa']="abcdef"; // successful login
else 
{header("Location: login.php"); exit();}
}
?> 
<html>
<head>
<title>title</title>
</head>
<body>
<a href="page-a.php">Page A</a>
<a href="page-b.php">Page B</a>
<a href="page-c.php">Page C</a>
</body></html>

 

PAGE-A.PHP

<?
echo $_SESSION['aaa'];
//if ($_SESSION['aaa']!="abcdef")
//header("Location: login.php");
//exit();
?>
<html>
<head>
<title>title</title>
</head>
<body>
...

Link to comment
https://forums.phpfreaks.com/topic/222041-passing-session-to-the-next-page/
Share on other sites

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.