mike12255 Posted March 30, 2009 Share Posted March 30, 2009 Im going to include two peices of code the function file code, and the code with the form and the code calling the function. My problem is everyone i call the function it returns the error message but im using the right login information. login.php: <?php if (isset($_POST['Submit'])){ $error = login(); if (isset($error)){ echo '<table width="397" height="113" border="0" align="center" cellpadding="0" cellspacing="0">'; echo"<tr><td>$error</td></tr>"; echo "</table>"; } } ?> <table width="397" height="113" border="0" align="center" cellpadding="0" cellspacing="0"> <tr> <td><form name="form1" method="post" action="login.php"> <label> Username: <input type="text" name="username" title="username:"id="username"> </label></tr></td> <tr><td> <label> Password: <input type="password" name="password" id="password"> </label> </tr></td> <tr><td> <label> <input type="submit" name="Submit" id="Submit" align="right" value="Submit"> </form> </label> </td> </tr> </table> Functions.php: <?php include ("connect.php"); error_reporting(E_ALL); function login(){ $user = $_POST['username']; $pass = $_POST['password']; $sql = "SELECT * FROM tbl_users WHERE username = '$user' AND password = '$pass'"; $result = mysql_query($sql) or die (mysql_error()); $row = mysql_num_rows($result); if ($row == 1){ $_SESSION['user'] = $user; }else{ $error = "Error Invalid username or password"; return $error; } } ?> anyone know why i keep getting this error message? Quote Link to comment https://forums.phpfreaks.com/topic/151848-solved-login-function-not-working/ Share on other sites More sharing options...
JeanieTallis Posted March 31, 2009 Share Posted March 31, 2009 Is the error message "Error Invalid username or password" Also, Though, in the login.php file, shouldn't it call the functions.php file? ***EDIT*** If the error message is what I asked, then you wont need to call the functions.php file, yet if ($row == 1){ $_SESSION['user'] = $user; }else{ I believe should know what to do, I'm guessing it doesn't recognise what it is that it has to do. So it gives the error maybe? Quote Link to comment https://forums.phpfreaks.com/topic/151848-solved-login-function-not-working/#findComment-797382 Share on other sites More sharing options...
mike12255 Posted March 31, 2009 Author Share Posted March 31, 2009 i do include the functions.php file, its just now showen in the code. and yes the error message is "Invalid username or password" Quote Link to comment https://forums.phpfreaks.com/topic/151848-solved-login-function-not-working/#findComment-797390 Share on other sites More sharing options...
JeanieTallis Posted March 31, 2009 Share Posted March 31, 2009 Is there a specific page afte rbeing logged in users should go to? Quote Link to comment https://forums.phpfreaks.com/topic/151848-solved-login-function-not-working/#findComment-797399 Share on other sites More sharing options...
lonewolf217 Posted March 31, 2009 Share Posted March 31, 2009 well one problem I see is that in your login function it will not return a value if the login was successful. you would be better off returning TRUE (valid login) or FALSE (bad login) and just outputting a message based on the return value of the function. Quote Link to comment https://forums.phpfreaks.com/topic/151848-solved-login-function-not-working/#findComment-797411 Share on other sites More sharing options...
JeanieTallis Posted March 31, 2009 Share Posted March 31, 2009 Basically, what I was trying to get at, but you said it in more complicated terms. A Valid Login = Page to display to the user A Invalid Login = Error I think thats what you were getting at anyway lonewolf217. Quote Link to comment https://forums.phpfreaks.com/topic/151848-solved-login-function-not-working/#findComment-797414 Share on other sites More sharing options...
Andy-H Posted March 31, 2009 Share Posted March 31, 2009 login.php: <?php // session_start(); /* REMOVE QUOTES IF SESSION_START ISNT USED IN CONNECT.PHP TO INITIALISE SESSION */ require_once "functions.php"; if (isset($_POST['Submit'])){ $error = login(); if (isset($error)){ echo '<table width="397" height="113" border="0" align="center" cellpadding="0" cellspacing="0">'; echo"<tr><td>$error</td></tr>"; echo "</table>"; } } if ( !isSet($_SESSION['user']) ){ ?> <table width="397" height="113" border="0" align="center" cellpadding="0" cellspacing="0"> <tr> <td><form name="form1" method="post" action="login.php"> <label> Username: <input type="text" name="username" title="username:"id="username"> </label></tr></td> <tr><td> <label> Password: <input type="password" name="password" id="password"> </label> </tr></td> <tr><td> <label> <input type="submit" name="Submit" id="Submit" align="right" value="Submit"> </form> </label> </td> </tr> </table> <?php } ?> Functions.php: <?php include ("connect.php"); error_reporting(E_ALL); function login(){ $user = mysql_real_escape_string($_POST['username']); $pass = mysql_real_escape_string($_POST['password']); $sql = "SELECT * FROM tbl_users WHERE username = '$user' AND password = '$pass'"; $result = mysql_query($sql) or die (mysql_error()); $row = mysql_num_rows($result); if ($row == 1){ $_SESSION['user'] = $user; // Header("Location: logged_in.php"); }else{ $error = "Error Invalid username or password"; return $error; } } ?> Quote Link to comment https://forums.phpfreaks.com/topic/151848-solved-login-function-not-working/#findComment-797441 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.