budder Posted April 14, 2010 Share Posted April 14, 2010 Hey PhpFreaks (: Got a problem with my log in script. When I type the username and password it only returns the log in page. I can't find the error and what causing the script not to log in. It returns the first login form, not the second as it should if the pass or user is wrong. <? session_start(); // start session. ?> <!-- header tags, edit to match your own, or include template header file. --> <html> <head> <title>Login</title> <head> <body> <? if(!isset($username) | !isset($password)) { // escape from php mode. ?> <form action="<?=$PHP_SELF?><?if($QUERY_STRING){ echo"?". $QUERY_STRING;}?>" method="POST"> <p align="center">Members only. Please login to access this document.</p> <table align="center" border="0"> <tr> <th> Username: </th> <th> <input type="text" name="username"> </th> </tr> <tr> <th> Password: </th> <th> <input type="password" name="password"> </th> </tr> <tr> <th colspan="2" align="right"> <input type="submit" value="Login"> </form> </th> </tr> </table> </body> </html> <? exit(); } // If all is well so far. session_register("username"); session_register("password"); // register username and password as session variables. $dbHost = "sql01.dk"; $dbUser = "elmerdahldk"; $dbPass = "rNnj5Tc2"; $dbDatabase = "elmerdahldk"; $mysqli = mysql_connect("$dbHost", "$dbUser", "$dbPass"); mysql_select_db($dbDatabase, $mysqli) or die("Can not connect"); // Check connection if (mysql_error()) { printf("Unable to connect to database: %s", mysql_error()); exit(); } $sql = mysql_query("SELECT password FROM users WHERE username = '$username'"); $fetch_em = mysql_fetch_array($sql); $numrows = mysql_num_rows($sql); if($numrows != "0" & $password == $fetch_em["password"]) { $valid_user = 1; } else { $valid_user = 0; } // If the username exists and pass is correct, don't pop up the login code again. // If info can't be found or verified.... if (!($valid_user)) { session_unset(); // Unset session variables. session_destroy(); // End Session we created earlier. // escape from php mode. ?> <form action="<?=$PHP_SELF?><?if($QUERY_STRING){ echo"?". $QUERY_STRING;}?>" method="POST"> <p> align="center"><b>Forkert brugernavn eller kode</b></p> <p align="center">Incorrect login information, please try again. You must login to access this document.</p> <table align="center" border="0"> <tr> <th> Username: </th> <th> <input type="text" name="username"> </th> </tr> <tr> <th> Password: </th> <th> <input type="password" name="password"> </th> </tr> <tr> <th colspan="2" align="right"> <input type="submit" value="Login"> </form> </th> </tr> </table> </body> </html> <? exit(); } ?> Quote Link to comment Share on other sites More sharing options...
Deoctor Posted April 14, 2010 Share Posted April 14, 2010 action shoould be some thing like this action="<?php echo $_SERVER['PHP_SELF'];?>" Quote Link to comment Share on other sites More sharing options...
budder Posted April 14, 2010 Author Share Posted April 14, 2010 Hmm, doesn't seems to fix anything, still getting the fist form. hmm. Quote Link to comment Share on other sites More sharing options...
Deoctor Posted April 14, 2010 Share Posted April 14, 2010 i am just now checking ur form, lots of bugs are there, fixing it out.. Quote Link to comment Share on other sites More sharing options...
Deoctor Posted April 14, 2010 Share Posted April 14, 2010 try this <? error_reporting(0); session_start(); // start session. ?> <!-- header tags, edit to match your own, or include template header file. --> <html> <head> <title>Login</title> </head> <body> <form action="<?php echo $_SERVER['PHP_SELF'];?>" method="POST"> <p align="center">Members only. Please login to access this document.</p> <table align="center" border="0"> <tr> <th> Username: </th> <th> <input type="text" name="username"> </th> </tr> <tr> <th> Password: </th> <th> <input type="password" name="password"> </th> </tr> <tr> <th colspan="2" align="right"> <input type="submit" name="submit" value="Login"> </th> </tr> </table> </form> </body> </html> <? // If all is well so far. if(isset($_POST['submit'])) { echo "Hai"; $username=$_POST['username']; $password=$_POST['password']; session_register("username"); session_register("password"); // register username and password as session variables. $dbHost = "sql01.dk"; $dbUser = "elmerdahldk"; $dbPass = "rNnj5Tc2"; $dbDatabase = "elmerdahldk"; $mysqli = mysql_connect("$dbHost", "$dbUser", "$dbPass"); mysql_select_db($dbDatabase, $mysqli) or die("Can not connect"); // Check connection if (mysql_error()) { printf("Unable to connect to database: %s", mysql_error()); exit(); } $sql = mysql_query("SELECT * FROM users WHERE username = '$username' and password='$password'"); //$fetch_em = mysql_fetch_array($sql); $numrows = mysql_num_rows($sql); if($numrows != "0") { $valid_user = 1; } else { $valid_user = 0; } // If the username exists and pass is correct, don't pop up the login code again. // If info can't be found or verified.... if ($valid_user == 0) { echo " sorry wrong details"; session_unset(); // Unset session variables. session_destroy(); // End Session we created earlier. // escape from php mode. } else { echo "Success"; } } ?> Quote Link to comment Share on other sites More sharing options...
budder Posted April 14, 2010 Author Share Posted April 14, 2010 Thank you, now it's working Is it possible to hide the login form when login success = true ? Quote Link to comment Share on other sites More sharing options...
Deoctor Posted April 14, 2010 Share Posted April 14, 2010 Thank you, now it's working Is it possible to hide the login form when login success = true ? u redirect to some other page after that Quote Link to comment Share on other sites More sharing options...
budder Posted April 14, 2010 Author Share Posted April 14, 2010 Yeah you are right. Thanks (: Quote Link to comment 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.