asmith Posted November 19, 2007 Share Posted November 19, 2007 my login page with session don't work !! login: . . . if ($num_rows != 0) {$auth="<a href=\"hostgame.php\">host a game!</a>"; session_start(); session_register('abc'); $_SESSION[abc]="bbb"; } else {$auth="bad login";} ?> <html> <head> </head> <body> <?php echo $auth; ?> </body </html> a.php : <?php if ($_SESSION[abc] == "bbb") {$msg="hello !";} else {header("location: /my2/login.html"); exit; } ?> <html> . . . it shows login page again ! please help !! Quote Link to comment Share on other sites More sharing options...
gerkintrigg Posted November 19, 2007 Share Posted November 19, 2007 try using single quotes for echo and variable statements like $auth="<a href=\"hostgame.php\">host a game![/url]"; it avoids needing to add backslashes into the HTML. Also laying out the code a little better might help spot errors... Regarding your problem: what I do is open the session at the top of the page, check the posted details against the database to see if a record exists, count the number of rows, if the number of rows >=1 then log the user in by setting a couple of sessions (normally a session called "Logged_In" and one called "my_id" (to act as a short-cut to find out the database id number of the user) before using a header redirect to the user's control panel. the control panel (and all subsequent member-only pages) also have a session open statement, followed by a session "Logged_In" member check with a redirect for anyone who has not registered the logged in session. It tends to work very well for me. This also allows the login page itself to have all the login code at the top of the page, followed by error checks and appropriate messages, in case the redirect script was not triggered. I hope this helps... but it may involve having to write all of it again. (Sorry) Quote Link to comment Share on other sites More sharing options...
asmith Posted November 19, 2007 Author Share Posted November 19, 2007 i understand what you said , could you please give some example codes ? and some lines i wrote before is wrong , here is the original on my pc : login: . . . if ($num_rows != 0) {$auth="<a href=\"a.php\">host a game!</a>"; session_start(); session_register('abc'); $_SESSION[abc]="bbb"; } else {$auth="bad login";} ?> <html> <head> </head> <body> <?php echo $auth; ?> </body </html> a.php : <?php if ($_SESSION[abc] == "bbb") {$msg="hello !";} else {header("location: /my2/login.html"); exit; } ?> <html> . . . Quote Link to comment Share on other sites More sharing options...
trq Posted November 19, 2007 Share Posted November 19, 2007 Can you please use [ code ][ /code ] tags (without the spaces) when posting code. Also, as I said before, session_register() has long been depricated. I'm not sure how you logging users in as there doesn't appear to be any check, but I assume this is jus some sort of test code. You might want to post the rest of your code so we can see whats happening, also a description of your actual issue would help. Quote Link to comment Share on other sites More sharing options...
pkSML Posted November 19, 2007 Share Posted November 19, 2007 session_start(); --> Do this at the beginning of all your PHP pages. That's what gerkintrigg was saying. It should solve your problem immediately. Quote Link to comment Share on other sites More sharing options...
asmith Posted November 19, 2007 Author Share Posted November 19, 2007 ok , here the total codes : <?php $conn=mysql_connect("localhost","john","1234"); $db=mysql_select_db("testdb",$conn); $sql="select * from users where username='$_POST[username]' and password='$_POST[password]'"; $sc=mysql_query($sql,$conn) or die(mysql_error()); $nums=mysql_num_rows($sc); if ($nums != 0) {$auth="[a href=\"a.php\"] a page![/a]"; session_start(); session_register('abc'); $_SESSION[abc]="bbb"; } else {$auth="bad login";} ?> <html> <head> </head> <body> <?php echo $auth; ?> </body> </html> a.php : <?php if ($_SESSION['abc'] == 'bbb') {$msg="hello!";} else {header("location: /my2/login.html"); exit; } ?> <html> <body> <?php echo $msg; ?> </body> </html> note i see the "a page" link , but when i click on it i see the login page again . thanks Quote Link to comment Share on other sites More sharing options...
asmith Posted November 19, 2007 Author Share Posted November 19, 2007 session_start(); --> Do this at the beginning of all your PHP pages. That's what gerkintrigg was saying. It should solve your problem immediately. it worked !! IE works fine, but when i close the FF , and open again it shows the page again , seems FF do not end the session after the browser is closed, is that bug ? what should i do to make it end when ff closes too ? Quote Link to comment Share on other sites More sharing options...
trq Posted November 19, 2007 Share Posted November 19, 2007 As has been said, you need session_start() in the top of a.php. Also, as has been said, session_register(0 has long been depricated. Try.. <?php $conn = mysql_connect("localhost","john","1234"); $db = mysql_select_db("testdb",$conn); $sql = "SELECT * FROM `users` WHERE `username` = '{$_POST['username']}' && `password`= '{$_POST['password']}'"; if ($sc = mysql_query($sql,$conn)) { if (mysql_num_rows($sc)) { $auth = "<a href=\"a.php\"> a page!</a>"; session_start(); $_SESSION['abc'] = "bbb"; } else { $auth = "bad login"; } } ?> <html> <head> </head> <body> <?php echo $auth; ?> </body> </html> a.php <?php session_start(); if (isset($_SESSION['abc']) && $_SESSION['abc'] == 'bbb') { $msg = "hello!"; } else { header("location: /my2/login.html"); exit; } ?> <html> <body> <?php echo $msg; ?> </body> </html> Quote Link to comment Share on other sites More sharing options...
trq Posted November 19, 2007 Share Posted November 19, 2007 what should i do to make it end when ff closes too ? As has also been said before. There is no guarentee a session will end when the browser is closed. Sessions have a set lifetime. Quote Link to comment Share on other sites More sharing options...
asmith Posted November 19, 2007 Author Share Posted November 19, 2007 where should i put session_cache_expire(30); ? before session starts? //at least if there is no garantee for closing browsers, set a time for that Quote Link to comment Share on other sites More sharing options...
trq Posted November 19, 2007 Share Posted November 19, 2007 session_cache_expire is an ini directive. You would need to place... ini_set('session_cache_expire','30'); prior to session_start(), it really shouldn't be needed though. Quote Link to comment Share on other sites More sharing options...
asmith Posted November 19, 2007 Author Share Posted November 19, 2007 the defualt is 180,right ? you meant i can change the defualt too ? (sorry i think it is my english bad ! :s) Quote Link to comment Share on other sites More sharing options...
pkSML Posted November 19, 2007 Share Posted November 19, 2007 Something else to think about... You can set the length of the session by using a persistent session. This length is from the end of the last logged-in request. This will effectively log them off after xx minutes of inactivity. Note that the length before expiry is user-side. It's up to their browser to comply with the length of the cookie. <?php $length = 3600; // length in seconds of session --> 3600 seconds = 1 hour session_set_cookie_params($length, "/", ".yourdomain.com"); session_start(); //Start session // Your PHP code here ?> Be sure to replace yourdomain.com with your domain name. 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.