Imothep Posted July 25, 2006 Share Posted July 25, 2006 Hello folks im a PHP noob and im having trouble with sessions.[code]<?php session_start();$host="xxxxxx";$username="xxxxxx";$password="xxxxx";$database="XXXXX";$larfuser=$_POST['email'];$larfpass=$_POST['password'];mysql_connect($host, $username, $password) or die("Could not connect to the server");mysql_select_db($database) or die("Could not connect to the database"); $sql=mysql_query("SELECT * FROM larf_users WHERE email='$larfuser' AND password='$larfpass'"); if (mysql_num_rows($sql)==0) { echo"Could not log you in."; exit; }else{ session_register("username"); $_SESSION['welcome'] = "Welcome to your control panel $larfuser"; header("location:http://www.pixelpeople.org/larf/usernames.php"); } ?>[/code]This site does what it is supposed to.. it takes me to the usernames.php if the usrname and pw corresponds to the information in the mysql db. However, when i try to logout on the usernames page and i try to press back in the browser i can still access usernames.php.This is my code for logout.php[code]<? session_unset("username");header("location:http://www.pixelpeople.org/larf/");echo "Successfully logged out";die;?>[/code]Please do not laugh.. i started doing PHP 2 weeks ago and im enjoying it :)Can anyone give me a noobfriendly explanation to what is wrong here and how i can correct it? Tips and Tricks are most welcome... heheThank you very much folks! Quote Link to comment https://forums.phpfreaks.com/topic/15575-having-trouble-with-sessions/ Share on other sites More sharing options...
Imothep Posted July 25, 2006 Author Share Posted July 25, 2006 [quote author=Imothep link=topic=101785.msg403116#msg403116 date=1153829775]Hello folks im a PHP noob and im having trouble with sessions.[code]<?php session_start();$host="xxxxxx";$username="xxxxxx";$password="xxxxx";$database="XXXXX";$larfuser=$_POST['email'];$larfpass=$_POST['password'];mysql_connect($host, $username, $password) or die("Could not connect to the server");mysql_select_db($database) or die("Could not connect to the database"); $sql=mysql_query("SELECT * FROM larf_users WHERE email='$larfuser' AND password='$larfpass'"); if (mysql_num_rows($sql)==0) { echo"Could not log you in."; exit; }else{ session_register("username"); $_SESSION['welcome'] = "Welcome to your control panel $larfuser"; header("location:http://www.pixelpeople.org/larf/usernames.php"); } ?>[/code]This site does what it is supposed to.. it takes me to the usernames.php if the usrname and pw corresponds to the information in the mysql db. However, when i try to logout on the usernames page and i try to press back in the browser i can still access usernames.php.How can i prevent that ?This is my code for logout.php[code]<? session_unset("username");header("location:http://www.pixelpeople.org/larf/");echo "Successfully logged out";die;?>[/code]Please do not laugh.. i started doing PHP 2 weeks ago and im enjoying it :)Can anyone give me a noobfriendly explanation to what is wrong here and how i can correct it? Tips and Tricks are most welcome... heheThank you very much folks![/quote] Quote Link to comment https://forums.phpfreaks.com/topic/15575-having-trouble-with-sessions/#findComment-63346 Share on other sites More sharing options...
king arthur Posted July 25, 2006 Share Posted July 25, 2006 Try adding a session_destroy() to your logout script, that may do the trick. Quote Link to comment https://forums.phpfreaks.com/topic/15575-having-trouble-with-sessions/#findComment-63352 Share on other sites More sharing options...
Imothep Posted July 25, 2006 Author Share Posted July 25, 2006 i tried adding session_destroy to my code [code]<? session_destroy("username");header("location:http://www.pixelpeople.org/larf/");echo "Successfully logged out";die;?>[/code]but it still doesnt work :S Quote Link to comment https://forums.phpfreaks.com/topic/15575-having-trouble-with-sessions/#findComment-63365 Share on other sites More sharing options...
Imothep Posted July 25, 2006 Author Share Posted July 25, 2006 Warning: Wrong parameter count for session_destroy() in /home/1/p/pixelpeople/www/larf/logout.php on line 1Warning: Cannot modify header information - headers already sent by (output started at /home/1/p/pixelpeople/www/larf/logout.php:1) in /home/1/p/pixelpeople/www/larf/logout.php on line 2Successfully logged outthat is the error i get Quote Link to comment https://forums.phpfreaks.com/topic/15575-having-trouble-with-sessions/#findComment-63367 Share on other sites More sharing options...
king arthur Posted July 25, 2006 Share Posted July 25, 2006 session_destroy() does not take a parameter.The problem is that when you hit the back button, your browser will re-send the $_POST vars, so the $_POST['email'] and $_POST['password'] will be available in the script again. The browser would normally warn you about this. You should have a $_SESSION['loggedin'] variable that, if not present, causes the script to ask for the username and password, by destroying the session any such variable will be deleted. Don't just rely on whether the username is present in the $_POST vars. Quote Link to comment https://forums.phpfreaks.com/topic/15575-having-trouble-with-sessions/#findComment-63371 Share on other sites More sharing options...
Imothep Posted July 25, 2006 Author Share Posted July 25, 2006 Ok thank you King Arthur.. can you show me an example of how to do that using my script ? I would be very greatfull.. Thanks again Quote Link to comment https://forums.phpfreaks.com/topic/15575-having-trouble-with-sessions/#findComment-63374 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.