lanceox Posted April 23, 2011 Share Posted April 23, 2011 Hi there, i have a login page that does checks for things like incorrect passwords, etc however as i cant use die function as it terminates the rest of my code i used header(location).... to send the user back. is there anyway i could get the password checks to work without terminating my code with die()???? Thanks Lance Quote Link to comment https://forums.phpfreaks.com/topic/234520-use-of-die-function/ Share on other sites More sharing options...
fugix Posted April 23, 2011 Share Posted April 23, 2011 die() will terminate any code that is after it...I believe that it is mainly meant for when errors occur, so the script doesnt continue to run Quote Link to comment https://forums.phpfreaks.com/topic/234520-use-of-die-function/#findComment-1205240 Share on other sites More sharing options...
Pikachu2000 Posted April 23, 2011 Share Posted April 23, 2011 Using die() to handle user input errors is rarely a good way to do it. If you're validating, you should be able to repopulate and re-present the form with the user's input in the fields, and display an error message detailing the validation errors. Quote Link to comment https://forums.phpfreaks.com/topic/234520-use-of-die-function/#findComment-1205242 Share on other sites More sharing options...
lanceox Posted April 23, 2011 Author Share Posted April 23, 2011 ahh right okay, tried using die() as i couldnt think of a way around the problem but the user to be redirected to the previous page if login details are incorrect but display a message. Whenever i redirect the message wont display. This is the code if any needs to see <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.1//EN" "http://www.w3.org/TR/xhtml11/DTD/xhtml11.dtd"> <html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en"> <head> <title>Forensics E-learning Package</title> <script type="text/javascript" src="start.js"></script> <link rel="stylesheet" type="text/css" href="style.css" /> </head> <body> <div id="wrapper"> <div id="header"> <div id="toplinks"> </div> </div> <div id="menu"> <ul> <li><a class="selected" href="index.html">Home</a></li> <li><a href="index.php">Initial Quiz</a></li> <li><a href="about.php">About</a></li> <li><a href="member.php">Member Section</a></li> </ul> </div> <div id="content"> <div id="main"> <h1>Forensics E-Learning Package</h1><BR /></head> Login to the User Profiled E-Learning Course which is specifically aimed to raise awareness in computer forensics. <BR /><BR /> <form action='login.php' method='POST'> Username: <input type='text' name='username'><BR /> Password: <input type='password' name='password'><BR /> <input type='submit' value='Log In'> </form> <p><BR /><BR /> <a class="button" href='register.php'><span><button class="button" id="save">Register</button></span></a> </div> <div id="right"> <h2>Right Menu</h2> <div class="rightitem"> <ul> <li><a class="selected" href="index.html">Home</a></li> <li><a href="index.php">Initial Quiz</a></li> <li><a href="about.php">About</a></li> <li><a href="member.php">Members Area</a></li> <li><a href="contact.php">Leave Feedback</a></li> </ul> </div> </div> </div> <div class="clearbottom"></div> <div id="footer"></div></div> </body> </html> page that checks the login information <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.1//EN" "http://www.w3.org/TR/xhtml11/DTD/xhtml11.dtd"> <html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en"> <head> <title>Forensics E-learning Package</title> <link rel="stylesheet" type="text/css" href="style.css" /> </head> <body> <div id="wrapper"> <div id="header"> <div id="toplinks"> </div> </div> <div id="menu"> <ul> <li><a class="selected" href="home.html">Home</a></li> <li><a href="initialquiz.php">Initial Quiz</a></li> <li><a href="about.php">About</a></li> <li><a href="evidencemodule.php">Evidence Module</a></li> </ul> </div> <div id="content"> <div id="main"> <h1>Forensics E-Learning Package</h1><BR /> <?php session_start(); $username = $_POST['username']; $password = $_POST['password']; $_SESSION['$username'] = $username; $_SESSION['$password'] = sha1($password); if($username && sha1($password)) { $connect = mysql_connect("localhost","root", "") or die ("Couldn't Connect!"); mysql_select_db("userlogin") or die("Couldn't find db"); //$con = mysql_connect('userscores.db.7767668.hostedresource.com','userscores','L3tt3r09'); //mysql_select_db('userscores', $con); $query = mysql_query("SELECT * FROM users WHERE username='$username'"); $numrows = mysql_num_rows($query); if ($numrows!=0) { //code to login while ($row = mysql_fetch_assoc($query)) { $dbusername = $row['username']; $dbpassword = $row['password']; $dbscore = $row['score']; $dbdclty = $row['dclty']; $dbid = $row['id']; $dbnewdclty = $row['newdclty']; } //$scores = $dbscore; //$query1 = mysql_query("SELECT * FROM scores"); //while ($row = mysql_fetch_assoc($query)) //{ //$dbscores = $row['scores']; //$dbid = $row['id']; //} // $num=mysql_numrows($query1); if ($username==$dbusername&&sha1($password)==$dbpassword) { $username==$dbusername; } else { header('Location: index.php'); echo "<script>alert(\"incorrect password!\");</script>"; } } else { header('Location: index.php'); echo("That user doesn't exist!"); } } else { header('Location: index.php'); echo("Please enter a username and password!"); } $_SESSION['id'] = $dbid; $_SESSION['PreviousScore'] = $dbscore; $_SESSION['dclty'] = $dbdclty; $_SESSION['newdclty'] = $dbnewdclty; ?> <h2>Welcome to the Computer Forensics e-Learning Package</h2><BR /><BR /> Hi <b><?php echo $username; ?></b> <BR /><BR /> <p><b>Traditional E-Learning Packages</b><BR /> <BR /> </p> <p> Traditional e-learning systems are typically informative systems and generally just present information to the user from page to page, which is followed by a final quiz, which tests your knowledge on the subject topic being learned. The main issues with traditional e-learning systems is that the information presented to the user is non-personalized and there is a general assumption that every learner is on the same page in terms of knowledge level. <br /> <br /> <b>The User Personalized Learning Package</b><BR /><BR /> User Personalization over comes issues with traditional e-learning systems through the use of user profiles, for example, rather than viewing information from slide to slide, the user must first take a quiz before hand. This will allow the user profiling component to calculate the score from the quiz and present the user with relevant learning material based upon the users results. <br /> <p> <p>Please take the test to receive personalized e-learning content by selecting a difficulty. <BR /><BR /> <table> <tr> <td> <form name="difficulty selection" method="POST" action="initialquiz.php"> <input type="radio" name="dclty" id="dcltya" value="1">Beginner<BR> <input type="radio" name="dclty" id="dcltyb" value="2">Intermediate<BR> <input type="radio" name="dclty" id="dcltyc" value="3">Expert<BR> <input type="submit" name="submitdclty" id="button" value="select a difficulty rating!" /> </form> <td> <tr> </table> <BR /> </div> <div id="right"> <h2>Right Menu</h2> <div class="rightitem"> <ul> <li><a class="selected" href="home.html">Home</a></li> <li><a href="initialquiz.php">Initial Quiz</a></li> <li><a href="about.php">About</a> </li> <li><a href="member.php">Members Area</a> </li> <li><a href="evidencemodule.php">Evidence Module</a></li> <li><a href="contact.php">Leave Feedback</a></li> <li><a href="logout.php">Logout</a> </li> </ul> <p> </p> </div><em></em> </div> </div> <div class="clearbottom"></div> <div id="footer"> <p id="legal"> </p> </div> </div> </div> </body> </html> Any help would be great. really stuck on this Thanks Lance Quote Link to comment https://forums.phpfreaks.com/topic/234520-use-of-die-function/#findComment-1205243 Share on other sites More sharing options...
fugix Posted April 23, 2011 Share Posted April 23, 2011 your echo needs to be before your header..you could use a <meta> refresh tag so that the user sees the message for a specified amount of time...then they are directed to the specified page Quote Link to comment https://forums.phpfreaks.com/topic/234520-use-of-die-function/#findComment-1205245 Share on other sites More sharing options...
lanceox Posted April 23, 2011 Author Share Posted April 23, 2011 Thanks for quick replies. I changed the echo so its before, however its still the same. I tried meta tags and i dont really want to use meta as this will show the message to the user however when they login it will also show member content for example. any more ideas will be great Quote Link to comment https://forums.phpfreaks.com/topic/234520-use-of-die-function/#findComment-1205248 Share on other sites More sharing options...
Pikachu2000 Posted April 23, 2011 Share Posted April 23, 2011 For a relatively small, simple form like that, I usually handle the processing in the same script, or include() the form within the processing script if the form has been submitted. The way you have it set up now, there's no way a header() will work to send the user back to the form, since the output starts long before header() is called. The logic should look like: if( form has been submitted ) { validate form if( validation ok ) { run db query redirect to "you're now logged in" page if db query returns valid credentials } else { set flag to include/echo form and errors allow correction and resubmission. } } else { set flag to include/echo form } if( flag === TRUE ) { include 'form' } Doing it in that manner, you have nothing output to the browser until the form has been validated, which will allow you to use header() redirects (if needed) in the processing logic, and eliminates the need to store errors in a $_SESSION var when you redirect back to a form. Quote Link to comment https://forums.phpfreaks.com/topic/234520-use-of-die-function/#findComment-1205250 Share on other sites More sharing options...
lanceox Posted April 23, 2011 Author Share Posted April 23, 2011 Okay ill give that a go and try and implement it myself and ill be back to let you know. Thanks again Lance Quote Link to comment https://forums.phpfreaks.com/topic/234520-use-of-die-function/#findComment-1205255 Share on other sites More sharing options...
Pikachu2000 Posted April 23, 2011 Share Posted April 23, 2011 If you have problems/questions, just say so. One tip I'll give you up front is to store any validation errors in an array. You can then check to see if the array is empty to determine if the form validated. If it isn't empty, you can implode() the array to display the errors. Quote Link to comment https://forums.phpfreaks.com/topic/234520-use-of-die-function/#findComment-1205259 Share on other sites More sharing options...
lanceox Posted April 23, 2011 Author Share Posted April 23, 2011 I changed some things around and built it all in one which is similar to the register page but i cant see where the problem is, it is either not pulling information from database or assigning it to variables correctly <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.1//EN" "http://www.w3.org/TR/xhtml11/DTD/xhtml11.dtd"> <html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en"> <head> <title>Forensics E-learning Package</title> <script type="text/javascript" src="start.js"></script> <link rel="stylesheet" type="text/css" href="style.css" /> </head> <body> <div id="wrapper"> <div id="header"> <div id="toplinks"> </div> </div> <div id="menu"> <ul> <li><a class="selected" href="index.html">Home</a></li> <li><a href="index.php">Initial Quiz</a></li> <li><a href="about.php">About</a></li> <li><a href="member.php">Member Section</a></li> </ul> </div> <div id="content"> <div id="main"> <h1>Forensics E-Learning Package</h1><BR /></head> Login to the User Profiled E-Learning Course which is specifically aimed to raise awareness in computer forensics. <BR /><BR /> <form action='index.php' method='POST'> Username: <input type='text' name='username'><BR /> Password: <input type='password' name='password'><BR /> <input type='submit' value='Log In'> </form> <p><BR /><BR /> <a class="button" href='register.php'><span><button class="button" id="save">Register</button></span></a> <?php $submit =&$_POST['submit']; if($submit) { if($username && sha1($password)) { $username =&$_POST['username']; $password =&$_POST['password']; $_SESSION['$username'] = $username; $_SESSION['$password'] = sha1($password); $connect = mysql_connect("localhost","root", "") or die ("Couldn't Connect!"); mysql_select_db("userlogin", $connect) or die("Couldn't find db"); //$con = mysql_connect('userscores.db.7767668.hostedresource.com','userscores','L3tt3r09'); //mysql_select_db('userscores', $con); $query = mysql_query("SELECT * FROM users WHERE username=' $username'"); $numrows = mysql_num_rows($query); if ($numrows!=0) { //code to login while ($row = mysql_fetch_assoc($query)) { $dbusername = $row['username']; $dbpassword = $row['password']; $dbscore = $row['score']; $dbdclty = $row['dclty']; $dbid = $row['id']; $dbnewdclty = $row['newdclty']; } $_SESSION['id'] = $dbid; $_SESSION['PreviousScore'] = $dbscore; $_SESSION['dclty'] = $dbdclty; $_SESSION['newdclty'] = $dbnewdclty; if ($username==$dbusername&&sha1($password)==$dbpassword) { $username==$dbusername; } else { echo ("Incorrect Password!"); } } else { echo("That user doesn't exist!"); } } else { echo("Please enter a username and password!"); } echo ("You Successfully Logged In!"); } else { //echo("Problem Logging In!"); } ?> </div> <div id="right"> <h2>Right Menu</h2> <div class="rightitem"> <ul> <li><a class="selected" href="index.html">Home</a></li> <li><a href="index.php">Initial Quiz</a></li> <li><a href="about.php">About</a></li> <li><a href="member.php">Members Area</a></li> <li><a href="contact.php">Leave Feedback</a></li> </ul> </div> </div> </div> <div class="clearbottom"></div> <div id="footer"></div></div> </body> </html> thanks for all your help so far lance Quote Link to comment https://forums.phpfreaks.com/topic/234520-use-of-die-function/#findComment-1205365 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.