squiblo Posted July 23, 2009 Share Posted July 23, 2009 hi, im quite a noob to php and i need some help with login, what i want is, an error message to appear on the same page as the login page saying something like "Wrong username or password" when someone has made a mistake logging in. "session_register("myusername"); session_register("mypassword"); header("location:profile.php"); } else { echo "Wrong Username or Password"; }" this script i have directs me to another page while i want the error message to appear on the same page, does anyone have a solution? Quote Link to comment https://forums.phpfreaks.com/topic/167134-solved-login-error-message/ Share on other sites More sharing options...
waynew Posted July 23, 2009 Share Posted July 23, 2009 Hi. The script you've given seems to redirect ONLY is the login is correct. Otherwise, it seems to just echo out "Wrong Username or Password". Also, session_register has been depreciated. Quote Link to comment https://forums.phpfreaks.com/topic/167134-solved-login-error-message/#findComment-881256 Share on other sites More sharing options...
squiblo Posted July 23, 2009 Author Share Posted July 23, 2009 will i have to change my script completly? Quote Link to comment https://forums.phpfreaks.com/topic/167134-solved-login-error-message/#findComment-881260 Share on other sites More sharing options...
waynew Posted July 23, 2009 Share Posted July 23, 2009 What is the problem with it exactly? It seems to be doing what you want it to be doing? Quote Link to comment https://forums.phpfreaks.com/topic/167134-solved-login-error-message/#findComment-881264 Share on other sites More sharing options...
squiblo Posted July 23, 2009 Author Share Posted July 23, 2009 when an incorrect username or password is entered it directs me to a DIFFERENT page showing "Incorrect username or password", but what i want is "Incorrect username or password" to show on the same page next to the "login" button Quote Link to comment https://forums.phpfreaks.com/topic/167134-solved-login-error-message/#findComment-881268 Share on other sites More sharing options...
mmarif4u Posted July 23, 2009 Share Posted July 23, 2009 Would you like to share your code, we better analyze it. Quote Link to comment https://forums.phpfreaks.com/topic/167134-solved-login-error-message/#findComment-881271 Share on other sites More sharing options...
squiblo Posted July 23, 2009 Author Share Posted July 23, 2009 <?php $host="localhost"; // Host name $username=""; // Mysql username $password=""; // Mysql password $db_name=""; // Database name $tbl_name="members"; // Table name // Connect to server and select databse. mysql_connect("$host", "$username", "$password")or die("cannot connect"); mysql_select_db("$db_name")or die("cannot select DB"); // username and password sent from form $myusername=$_POST['myusername']; $mypassword=$_POST['mypassword']; // To protect MySQL injection (more detail about MySQL injection) $myusername = stripslashes($myusername); $mypassword = stripslashes($mypassword); $myusername = mysql_real_escape_string($myusername); $mypassword = mysql_real_escape_string($mypassword); $sql="SELECT * FROM $tbl_name WHERE username='$myusername' and password='$mypassword'"; $result=mysql_query($sql); // Mysql_num_row is counting table row $count=mysql_num_rows($result); // If result matched $myusername and $mypassword, table row must be 1 row if($count==1){ // Register $myusername, $mypassword and redirect to file "profile.php" session_register("myusername"); session_register("mypassword"); header("location:profile.php"); } else { echo "Wrong Username or Password"; } ?> Quote Link to comment https://forums.phpfreaks.com/topic/167134-solved-login-error-message/#findComment-881273 Share on other sites More sharing options...
waynew Posted July 23, 2009 Share Posted July 23, 2009 Post the code from the file with the actual login form on it. Quote Link to comment https://forums.phpfreaks.com/topic/167134-solved-login-error-message/#findComment-881274 Share on other sites More sharing options...
mmarif4u Posted July 23, 2009 Share Posted July 23, 2009 If you input wrong username or pass. it should not pass u to another page. Can you post the form code too. Quote Link to comment https://forums.phpfreaks.com/topic/167134-solved-login-error-message/#findComment-881277 Share on other sites More sharing options...
squiblo Posted July 23, 2009 Author Share Posted July 23, 2009 <form name="form1" method="post" action="checklogin.php"> <input type="text" id="myusername" style="position:absolute;left:651px;top:59px;width:174px;font-family:Arial;font-size:13px;z-index:4" size="29" name="myusername" value="" title="username"> <input type="password" id="mypassword" style="position:absolute;left:651px;top:86px;width:174px;font-family:Arial;font-size:13px;z-index:5" size="29" name="mypassword" value="" tabindex="password" title="password"> <input type="checkbox" id="Checkbox1" name="Checkbox1" value="on" style="position:absolute;left:648px;top:113px;font-family:Arial;font-size:13px;z-index:6"> <input type="submit" id="Button1" name="Login" value="Login" style="position:absolute;left:778px;top:117px;width:50px;height:43px;font-family:Arial;font-size:13px;z-index:8" title="Login"> </form> Quote Link to comment https://forums.phpfreaks.com/topic/167134-solved-login-error-message/#findComment-881278 Share on other sites More sharing options...
mmarif4u Posted July 23, 2009 Share Posted July 23, 2009 Try this: <?php $host="localhost"; // Host name $username=""; // Mysql username $password=""; // Mysql password $db_name=""; // Database name $tbl_name="members"; // Table name // Connect to server and select databse. mysql_connect("$host", "$username", "$password")or die("cannot connect"); mysql_select_db("$db_name")or die("cannot select DB"); if(isset($_POST['Login'])){ // username and password sent from form $myusername=$_POST['myusername']; $mypassword=$_POST['mypassword']; // To protect MySQL injection (more detail about MySQL injection) $myusername = stripslashes($myusername); $mypassword = stripslashes($mypassword); $myusername = mysql_real_escape_string($myusername); $mypassword = mysql_real_escape_string($mypassword); $sql="SELECT * FROM $tbl_name WHERE username='$myusername' and password='$mypassword'"; $result=mysql_query($sql); // Mysql_num_row is counting table row $count=mysql_num_rows($result); // If result matched $myusername and $mypassword, table row must be 1 row if($count==1){ // Register $myusername, $mypassword and redirect to file "profile.php" session_register("myusername"); session_register("mypassword"); header("location:profile.php"); } else { $error= "Wrong Username or Password"; } } ?> And: <form name="form1" method="post" action="checklogin.php"> <input type="text" id="myusername" style="position:absolute;left:651px;top:59px;width:174px;font-family:Arial;font-size:13px;z-index:4" size="29" name="myusername" value="" title="username"> <input type="password" id="mypassword" style="position:absolute;left:651px;top:86px;width:174px;font-family:Arial;font-size:13px;z-index:5" size="29" name="mypassword" value="" tabindex="password" title="password"> <input type="checkbox" id="Checkbox1" name="Checkbox1" value="on" style="position:absolute;left:648px;top:113px;font-family:Arial;font-size:13px;z-index:6"> <input type="submit" id="Button1" name="Login" value="Login" style="position:absolute;left:778px;top:117px;width:50px;height:43px;font-family:Arial;font-size:13px;z-index:8" title="Login"> </form> <?php echo $error ?> Quote Link to comment https://forums.phpfreaks.com/topic/167134-solved-login-error-message/#findComment-881283 Share on other sites More sharing options...
squiblo Posted July 23, 2009 Author Share Posted July 23, 2009 it directs me to a blank page, view for yourself go to www.squiblo.com. the page is protected because its not finished, the password to view the site is "allsaints". Quote Link to comment https://forums.phpfreaks.com/topic/167134-solved-login-error-message/#findComment-881289 Share on other sites More sharing options...
mmarif4u Posted July 23, 2009 Share Posted July 23, 2009 If you enter username and pass correctly, what it do? Quote Link to comment https://forums.phpfreaks.com/topic/167134-solved-login-error-message/#findComment-881297 Share on other sites More sharing options...
squiblo Posted July 23, 2009 Author Share Posted July 23, 2009 it goes to this // Check if session is not registered , redirect back to main page. // Put this code in first line of web page. <? session_start(); if(!session_is_registered(myusername)){ header("location:index.php"); } ?> <html> <body> Login Successful </body> </html> Quote Link to comment https://forums.phpfreaks.com/topic/167134-solved-login-error-message/#findComment-881300 Share on other sites More sharing options...
mmarif4u Posted July 23, 2009 Share Posted July 23, 2009 So it mean, it is working properly for correct username and pass. As i see from ur code. You are using checklogin.php to check username and pass. After successful login i will go to profile page. Am i correct? So u need to redirect the user again to index page upon wrong input. Just put header in your else statement for index page and see, what it doing... Quote Link to comment https://forums.phpfreaks.com/topic/167134-solved-login-error-message/#findComment-881310 Share on other sites More sharing options...
squiblo Posted July 23, 2009 Author Share Posted July 23, 2009 i change my else to: else { header("location: http://www.google.com"); } } ?> i used google just to test and it works, so i will just create a new page with another login form Quote Link to comment https://forums.phpfreaks.com/topic/167134-solved-login-error-message/#findComment-881315 Share on other sites More sharing options...
mmarif4u Posted July 23, 2009 Share Posted July 23, 2009 You can create a page for wrong input and show a login page to key in again. Quote Link to comment https://forums.phpfreaks.com/topic/167134-solved-login-error-message/#findComment-881319 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.