raghu_tak Posted June 11, 2013 Share Posted June 11, 2013 hi friendsi am learning php from head first php booki have written a login script code from book that is similar to itbut when i submit the user infoit display again this login script.but when i run this book's code it works fine.the both script are similar.the following is my code and next to it is book's.My code:-<?phpsession_start();$errormsg="";if(!isset($_SESSION['user_id'])){ if(isset($_POST['submit'])) { $dbc=mysqli_connect('localhost','root','','mismatch') or die('Error connecting database'); $uname=mysqli_real_escape_string($dbc,trim($_POST['user_name'])); $pword=mysqli_real_escape_string($dbc,trim($_POST['pass_word'])); if((!empty($uname))&&(!empty($pword))) { $query="SELECT * FROM 'mismatch'.'mismatch_user' WHERE username='$uname' AND password=SHA('$pword')"; $result=mysqli_query($dbc,$query) or die('Error quering database'); if(mysqli_num_rows($result) == 1) { $row=mysqli_fetch_array($result); $_SESSION['user_id']=$row['user_id']; $_SESSION['username']=$row['username']; echo'You have successfully logged in as '.$_SESSION['username'].'.<br/>'; echo'<a href=edit.php>Edit your profile</a>'; mysqli_close($dbc); } else { $errormsg='Sorry try different user name.'; } } else { $errormsg='Please enter all the information to log in.'; } }}?><html><head><title>Log in</title></head><body><h1 style="color:Darkcyan"><center>Welcome to Mismatch.com</center></h1><p><center><i>Please enter your log in info to access your page.</i></center></p><?phpif(empty($_SESSION['user_id'])){ echo $errormsg;?><form method="post" action="<?php echo $_SERVER['PHP_SELF']; ?>"><fieldset><legend>Log in</legend><label for="user_name">Username:</label><input type="text" name="user_name" value="<?php if(!empty($uname)) echo $uname; ?>"/><label for="pass_word">Password:</label><input type="password" name="pass_word"/></fieldset><input type="submit" name="login" value="Log in"></form><?php}?></body></html>and here is book's code<?php // Start the session session_start(); // Clear the error message $error_msg = ""; // If the user isn't logged in, try to log them in if (!isset($_SESSION['user_id'])) { if (isset($_POST['submit'])) { // Connect to the database $dbc = mysqli_connect('localhost','root','','mismatch'); // Grab the user-entered log-in data $user_username = mysqli_real_escape_string($dbc, trim($_POST['username'])); $user_password = mysqli_real_escape_string($dbc, trim($_POST['password'])); if (!empty($user_username) && !empty($user_password)) { // Look up the username and password in the database $query = "SELECT user_id, username FROM mismatch_user WHERE username = '$user_username' AND password = SHA('$user_password')"; $data = mysqli_query($dbc, $query); if (mysqli_num_rows($data) == 1) { // The log-in is OK so set the user ID and username session vars (and cookies), and redirect to the home page $row = mysqli_fetch_array($data); $_SESSION['user_id'] = $row['user_id']; $_SESSION['username'] = $row['username']; echo'you have successfully logged in as '.$_SESSION['username'].'.<br/>'; echo'<a href=edit.php>Edit your profile</a>'; } else { // The username/password are incorrect so set an error message $error_msg = 'Sorry, you must enter a valid username and password to log in.'; } } else { // The username/password weren't entered so set an error message $error_msg = 'Sorry, you must enter your username and password to log in.'; } } }?><html><head> <title>Mismatch - Log In</title> </head><body> <h3>Mismatch - Log In</h3><?php // If the session var is empty, show any error message and the log-in form; otherwise confirm the log-in if (empty($_SESSION['user_id'])) { echo $error_msg;?> <form method="post" action="<?php echo $_SERVER['PHP_SELF']; ?>"> <fieldset> <legend>Log In</legend> <label for="username">Username:</label> <input type="text" name="username" value="<?php if (!empty($user_username)) echo $user_username; ?>" /><br /> <label for="password">Password:</label> <input type="password" name="password" /> </fieldset> <input type="submit" value="Log In" name="submit" /> </form><?php }?></body></html>please help me friends!! Link to comment https://forums.phpfreaks.com/topic/279033-php-login-script-not-workingplease-help/ Share on other sites More sharing options...
mac_gyver Posted June 11, 2013 Share Posted June 11, 2013 you changed the name of your submit button in the form, so the code if(isset($_POST['submit])) no longer works. Link to comment https://forums.phpfreaks.com/topic/279033-php-login-script-not-workingplease-help/#findComment-1435329 Share on other sites More sharing options...
raghu_tak Posted June 11, 2013 Author Share Posted June 11, 2013 hell yeahit was silly mistake thanks a lot Link to comment https://forums.phpfreaks.com/topic/279033-php-login-script-not-workingplease-help/#findComment-1435332 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.