infernon Posted March 13, 2007 Share Posted March 13, 2007 I am currently attempting to pull a value from a server variable across three pages, but I'm having a problem. On my login page, I am using the following code: <?php session_unset(); ?> <html> <head> <title>Please log in.</title> </head> <body> <form method="post" action="test1.php"> <p>Enter your username: <input type="text" name="user"> </p> <p>Enter your password: <input type="password" name="pass"> </p> <p> <input type="submit" name="Submit" value="Submit") </p> </form> </body> </html> On test1.php, I am using the following code to check that the login information is correct: <?php session_start(); $_SESSION['username'] = $_POST['user']; $_SESSION['userpass'] = $_POST['pass']; $_SESSION['authuser'] = 0; if (($_SESSION['username'] == 'Joe') and ($_SESSION['userpass'] == '12345')) { $_SESSION['authuser'] == 1;} else { echo "Sorry, but you don't have permission to view this page."; exit(); } ?> On this page, there is also a link to another page that also checks for a properly authenticated user with the previous credentials. The problem that I'm having is that when someone clicks on the link, they receive an error message telling them that they do not have permission to view the page. Here is the code from the page at the end of the link: <?php session_start(); if ($_SESSION['authuser'] !=1) { echo "Sorry, but you don't have permission to view this page."; exit(); } ?> I am thinking that this problem is related to a server configuration issue, but I can't seem to put my finger on it. I don't know why the information in the session variable will be held for only one page. Quote Link to comment Share on other sites More sharing options...
AV1611 Posted March 13, 2007 Share Posted March 13, 2007 try this and post the results: <?php session_start(); echo 'SESSION IS SET TO: ".$_SESSION['authuser']l."<br/><br/>"; if ($_SESSION['authuser'] ==1){} else { echo "Sorry, but you don't have permission to view this page."; exit(); } ?> I know it seems silly but I'm trying to see what's actually happening... Quote Link to comment Share on other sites More sharing options...
infernon Posted March 13, 2007 Author Share Posted March 13, 2007 OK, which page should I put that code into? Quote Link to comment Share on other sites More sharing options...
AV1611 Posted March 13, 2007 Share Posted March 13, 2007 Sorry, you said it didn't work on the last page you mentioned in your post. Quote Link to comment Share on other sites More sharing options...
infernon Posted March 13, 2007 Author Share Posted March 13, 2007 It reads: SESSION IS SET TO: 0 Sorry, but you don't have permission to view this page. Why do you think that the value is getting lost? Is there a configuration change that needs to be made with apache? Quote Link to comment Share on other sites More sharing options...
virtuexru Posted March 13, 2007 Share Posted March 13, 2007 Why not just do this: $user = mysql_real_escape_string($_POST['user']); $pass = mysql_real_escape_string($_POST['pass']); if ($user == "x" && $pass == "x") { $_SESSION['auth'] = 1; $_SESSION['user'] = $user; $_SESSION['pass'] = $pass; } else { etc Quote Link to comment Share on other sites More sharing options...
infernon Posted March 13, 2007 Author Share Posted March 13, 2007 I'm not working with mysql yet. I was just trying to do this with PHP at the moment and the fact that the sessions aren't working is really bugging me. Quote Link to comment Share on other sites More sharing options...
virtuexru Posted March 13, 2007 Share Posted March 13, 2007 oo gotcha, well then just take out the "mysql_real_escape_string" part, and you should be alright anyway. Quote Link to comment Share on other sites More sharing options...
infernon Posted March 13, 2007 Author Share Posted March 13, 2007 Do you know why the code that I was using wouldn't work? Quote Link to comment Share on other sites More sharing options...
virtuexru Posted March 13, 2007 Share Posted March 13, 2007 if (($_SESSION['username'] == 'Joe') and ($_SESSION['userpass'] == '12345')) I'm pretty sure thats incorrect, don't know why, but thats not how I do it. Plus, its a lot more efficient to store things into variables ($) until you want to create sessions, makes the code a lot cleaner. It should probably be if ($_SESSION['username'] == 'Joe' && $_SESSION['userpass'] == '12345') not sure, that could be wrong too :\ Quote Link to comment Share on other sites More sharing options...
infernon Posted March 14, 2007 Author Share Posted March 14, 2007 Actually, I posted this on usenet and someone pointed out that I was using a comparison operator to assign the 1 value for 'authuser'. A single "equals" sign fixes the problem. Quote Link to comment Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.