Hooo Posted August 17, 2009 Share Posted August 17, 2009 I have been searching for hours for a problem which resembles mine with no luck... Basically I am starting my own little project and want a Login which works (Obviously...) Anyway, what I have so far is my index.php page has the login form on, you login and the "Login" button checks the checklogin.php page which does the SQL stuff and sets the session (I think), then of course if the User/Pass rows match the user is directed to login_success.php. Now this is all well and good, and the wrong User/Pass inputted it will tell you this with the else statement on checklogin.php. However, here is the problem, if I type the direct URL to login_success.php, it will still tell me Login Successful, even after ending the session. Now of course I want it so you can only access login_success.php if you have logged in, otherwise there is no point in needing to login right?, anyway, you should be redirected to index.php to relogin when trying to access this page, and I don't see why this isn't happening. Code is below: Checklogin.php <?php include 'config.php'; include 'opendb.php'; $tbl_name= 'Users'; $myusername=$_POST['usname']; $mypassword=$_POST['userpass']; $myusername = stripslashes($myusername); $mypassword = stripslashes($mypassword); $myusername = mysql_real_escape_string($myusername); $mypassword = mysql_real_escape_string($mypassword); $sql="SELECT * FROM " .$tbl_name ." WHERE usname='" . $myusername. "' and userpass='".$mypassword."'"; $result=mysql_query($sql); $count=mysql_num_rows($result); if($count == 1) { $_SESSION['usname'] = $myusername; echo '<meta http-equiv="refresh" content="2;url=http://www.WEBSITE.com/login_success.php">'; } else { echo "Wrong Username or Password"; } include 'closedb.php'; ?> login_success.php <? include 'config.php'; include 'opendb.php'; session_start(); if(!isset($_SESSION['usname'])) { header("location:index.php"); } ?> <html> <body> Login Successful<br /><br /> <a href="http://www.WEBSITE.com/logout.php">Logout</a> </body> </html> Any help on this would be so appreciated Link to comment https://forums.phpfreaks.com/topic/170744-solved-php-login-problem/ Share on other sites More sharing options...
pengu Posted August 17, 2009 Share Posted August 17, 2009 read down the code, try putting session_start(); if(!isset($_SESSION['usname'])) { header("location:index.php"); } before the includes Link to comment https://forums.phpfreaks.com/topic/170744-solved-php-login-problem/#findComment-900497 Share on other sites More sharing options...
deecee2000 Posted August 17, 2009 Share Posted August 17, 2009 You need to add below line in your first file. session_start(); Then try, it may work. Thanks, Link to comment https://forums.phpfreaks.com/topic/170744-solved-php-login-problem/#findComment-900499 Share on other sites More sharing options...
Cosizzle Posted August 17, 2009 Share Posted August 17, 2009 hmm might be old cookies or cache make sure you empty that. Also Ive always known header("location:") to be header("Location:") so perhaps its the lower case "l" Link to comment https://forums.phpfreaks.com/topic/170744-solved-php-login-problem/#findComment-900500 Share on other sites More sharing options...
Hooo Posted August 18, 2009 Author Share Posted August 18, 2009 deecee2000 you was spot on, thankyou. I firstly tried pengu's suggestion, however that simply made me redirect back to index.php even when I logged in correctly. Solved Link to comment https://forums.phpfreaks.com/topic/170744-solved-php-login-problem/#findComment-900535 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.