newphpcoder Posted September 9, 2010 Share Posted September 9, 2010 Good day! I have an index.php or a login page.The scenario on my webpage is when i successfully login and i accidentally press the back button the login page appear again and when i try to login again i could login again, which is not good. here is my code: <?php session_start(); if(isset($_SESSION['USER_ID'])){ exit("you can't login in again when your all ready logged!"); } //require_once 'conn.php'; $db_name="dspi"; mysql_connect("localhost", "root", "") or die("Cannot connect to server"); mysql_select_db("$db_name")or die("Cannot select DB"); $department = mysql_real_escape_string($_POST['department']); $username = mysql_real_escape_string($_POST['username']); $sql=mysql_query("SELECT `Department`, `Username` FROM `tbllogin` WHERE `Department` = '{$department}' AND Username = '{$username}'") or die(mysql_error()); $ct = mysql_num_rows($sql); if($ct == 1) { $row = mysql_fetch_assoc($sql); if($row['Department']=='Accounting') { header('location: Company.php'); } elseif($row['Department']=='Engineering') { header('location: Company.php'); } elseif($row['Department']=='Finishing_Goods') { header('location: Company.php'); } elseif($row['Department']=='HRAD') { header('location: Company.php'); } elseif($row['Department']=='MIS') { header('location:Company.php'); } elseif($row['Department']=='Packaging_and_Design') { header('location:Company.php'); } elseif($row['Department']=='Production') { header('location:Company.php'); } elseif($row['Department']=='Purchasing_Logistic') { header('location:Company.php'); } elseif($row['Department']=='QA_and_Technical') { header('location:Company.php'); } elseif($row['Department']=='Supply_Chain') { header('location:Company.php'); } else { header('location:index.php'); echo"Incorrect Username or Department"; } } ?> Link to comment https://forums.phpfreaks.com/topic/212936-login-problem/ Share on other sites More sharing options...
nblackwood Posted September 9, 2010 Share Posted September 9, 2010 I've tried the code you're using, and it works for me. You can always try and have the page automatically log the user out if they go back to the login page. For example: <?php session_start(); if(isset($_SESSION['USER_ID'])) { unset($_SESSION['USER_ID']); } else { if(!isset($_SESSION['USER_ID'])) { $_SESSION['USER_ID'] = ''; header("location:index.php"); } } ?> If that's problematic for you, then I'm out of ideas. Your code should work for you. Hope it helps. Link to comment https://forums.phpfreaks.com/topic/212936-login-problem/#findComment-1109086 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.