usman07 Posted January 6, 2012 Share Posted January 6, 2012 I have created a login page, but i want the users to be directed to another page only if the login details are correct. How would i do this?any help is appreciate. Coding for the PHP is below: thank you. <?php $username = $_POST["username"]; $password = $_POST["password"]; //This if statement asks if the $username variable is set. If it is it executes the php script. Otherwise it echoes the login form. if(isset($username)){ if (!($username == " " && $password == " ")) { $connect = mysql_connect("","","") or die("Couldn't connect!"); mysql_select_db("") or die("Couldn't find db"); $query = mysql_query("SELECT * FROM users WHERE username='$username'"); $row = mysql_fetch_array($query); $numrows = mysql_num_rows($query); $dbusername = $row['username']; $dbpassword = $row['password']; } else { echo ('<div id="username"> <form action="" method="post"/> <font color="red"> Please enter a username and password </font> <table><tr><td> <img src="imgs/Log In/username.png" alt=""/> </td><td> <input type="text" size="30" name="username" style="background-color:transparent;" /> </td></tr></table> <table><tr><td> <img src="imgs/Log In/password.png" alt=""/> </td><td> <input type="password" name="password" size="30" /> </td></tr></table> <form id="submitb" action=""> <input type="submit" value="Log in" /> </form> <p class="register">Not yet a member? <a href="Form.html">Register Here</a>, its Free!</p> </div>'); } //check to see if they match if ($username == $dbusername && $password == $dbpassword) { echo "You Are Now Logged In, Welcome To AdobeTuts!"; } else echo ('<div id="username"> <form action="" method="post"/> <div id="new"> <font color="red"> Wrong Username Or Password, Please Try Again </font> </div> <table><tr><td> <img src="imgs/Log In/username.png" alt=""/> </td><td> <input type="text" size="30" name="username" style="background-color:transparent;" /> </td></tr></table> <table><tr><td> <img src="imgs/Log In/password.png" alt=""/> </td><td> <input type="password" name="password" size="30" /> </td></tr></table> <form id="submitb" action=""> <input type="submit" value="Log in" /> </form> <p class="register">Not yet a member? <a href="Form.html">Register Here</a>, its Free!</p> </div>'); } //This next bit echoes the login form unless the $username variable is set. else { echo ('<div id="username"> <form action="" method="post"/> <table><tr><td> <img src="imgs/Log In/username.png" alt=""/> </td><td> <input type="text" size="30" name="username" style="background-color:transparent;" /> </td></tr></table> <table><tr><td> <img src="imgs/Log In/password.png" alt=""/> </td><td> <input type="password" name="password" size="30" /> </td></tr></table> <form id="submitb" action=""> <input type="submit" value="Log in" /> </form> <p class="register">Not yet a member? <a href="imgs/Homepage tuts/Form.php">Register Here</a>, its Free!</p> </div>'); } ?> Quote Link to comment https://forums.phpfreaks.com/topic/254481-i-have-created-a-basic-login-using-php-but-how-do-i-link-it-to-another-page/ Share on other sites More sharing options...
wolfcry Posted January 6, 2012 Share Posted January 6, 2012 What I'd do is inside the if() here: if (!($username == " " && $password == " ")) I'd place the following if statement. if($username == $db_username && $password == $db_password){ header( 'Location: http://www.yoursite.com/new_page.html' ) ; } Quote Link to comment https://forums.phpfreaks.com/topic/254481-i-have-created-a-basic-login-using-php-but-how-do-i-link-it-to-another-page/#findComment-1304815 Share on other sites More sharing options...
usman07 Posted January 6, 2012 Author Share Posted January 6, 2012 Im new to php but im doing something wrong,iv done it like this but getting a syntax error? //check to see if they match if ($username == $dbusername && $password == $dbpassword) { header( 'Location: http://adobetuts.netai.net/log in success.html' ) ; } echo "You Are Now Logged In, Welcome To AdobeTuts!"; } Quote Link to comment https://forums.phpfreaks.com/topic/254481-i-have-created-a-basic-login-using-php-but-how-do-i-link-it-to-another-page/#findComment-1304817 Share on other sites More sharing options...
wolfcry Posted January 6, 2012 Share Posted January 6, 2012 what does the error say? From what I see, you have an extra '}' curly brace at the end. Quote Link to comment https://forums.phpfreaks.com/topic/254481-i-have-created-a-basic-login-using-php-but-how-do-i-link-it-to-another-page/#findComment-1304820 Share on other sites More sharing options...
usman07 Posted January 6, 2012 Author Share Posted January 6, 2012 iv replaced the if statement but it says: Parse error: syntax error, unexpected '}' in /home/a3073051/public_html/index.php on line 125 this is what i did with the code: //check to see if they match if($username == $db_username && $password == $db_password){ header( 'Location: http://www.yoursite.com/new_page.html' ) ; } echo "You Are Now Logged In, Welcome To AdobeTuts!"; Quote Link to comment https://forums.phpfreaks.com/topic/254481-i-have-created-a-basic-login-using-php-but-how-do-i-link-it-to-another-page/#findComment-1304821 Share on other sites More sharing options...
wolfcry Posted January 6, 2012 Share Posted January 6, 2012 As I stated in my post above yours, it's because you have one too many curly braces '}'. A dangler that's not encapsulating anything. Quote Link to comment https://forums.phpfreaks.com/topic/254481-i-have-created-a-basic-login-using-php-but-how-do-i-link-it-to-another-page/#findComment-1304823 Share on other sites More sharing options...
wolfcry Posted January 6, 2012 Share Posted January 6, 2012 This is how it should look. if (!($username == " " && $password == " ")){ if ($username == $dbusername && $password == $dbpassword) { header( 'Location: http://adobetuts.netai.net/log in success.html' ) ; } echo "You Are Now Logged In, Welcome To AdobeTuts!"; } Quote Link to comment https://forums.phpfreaks.com/topic/254481-i-have-created-a-basic-login-using-php-but-how-do-i-link-it-to-another-page/#findComment-1304825 Share on other sites More sharing options...
wolfcry Posted January 6, 2012 Share Posted January 6, 2012 I also wanted to point out, that unless the echo statement "You are now logged in, welcome to adobetuts!" is on (or referenced by a session) the page you're redirecting them too, the user won't see it because the code will immediately redirect them away from that page. I'd place that echo statement at the end in an else statement and tell the user they're NOT logged in if their username or password evaluates to false. Quote Link to comment https://forums.phpfreaks.com/topic/254481-i-have-created-a-basic-login-using-php-but-how-do-i-link-it-to-another-page/#findComment-1304827 Share on other sites More sharing options...
usman07 Posted January 6, 2012 Author Share Posted January 6, 2012 ok i copied the code above, now when i type in the correct user logins it comes up with another page (which i wanted), but now when i type in the wrong user details it doesnt come up with a error message instead it comes up with a message saying You Are Now Logged In, Welcome To Adobetuts! Quote Link to comment https://forums.phpfreaks.com/topic/254481-i-have-created-a-basic-login-using-php-but-how-do-i-link-it-to-another-page/#findComment-1304828 Share on other sites More sharing options...
wolfcry Posted January 6, 2012 Share Posted January 6, 2012 Lol, read my previous post above your last one. It explains why. Essentially, you're telling a failed request that it's actually a positive login. Change it to something like echo "Sorry. Incorrect user login details. Please try again."; or whatever you prefer. if (!($username == " " && $password == " ")){ if ($username == $dbusername && $password == $dbpassword) { header( 'Location: http://adobetuts.netai.net/log in success.html' ) ; } else { echo "Login information incorrect. Please try again."; } Quote Link to comment https://forums.phpfreaks.com/topic/254481-i-have-created-a-basic-login-using-php-but-how-do-i-link-it-to-another-page/#findComment-1304830 Share on other sites More sharing options...
usman07 Posted January 6, 2012 Author Share Posted January 6, 2012 lol oh ok, but where is it im suppose to change it? cuz i already have message saying 'incorrect username or password' but now it doesnt show. Quote Link to comment https://forums.phpfreaks.com/topic/254481-i-have-created-a-basic-login-using-php-but-how-do-i-link-it-to-another-page/#findComment-1304832 Share on other sites More sharing options...
wolfcry Posted January 6, 2012 Share Posted January 6, 2012 Lol, you keep beating me to the punch dang it! . Use the code I posted above and you should be fine. You can also use another redirect in that page or have the page refresh itself (which is what I do for long forms keeping all inputted values except for passwords etc.). Quote Link to comment https://forums.phpfreaks.com/topic/254481-i-have-created-a-basic-login-using-php-but-how-do-i-link-it-to-another-page/#findComment-1304834 Share on other sites More sharing options...
usman07 Posted January 6, 2012 Author Share Posted January 6, 2012 this is what i have just in this little section of the php code: if (!($username == " " && $password == " ")){ if ($username == $dbusername && $password == $dbpassword) { header( 'Location: http://adobetuts.netai.net/log in success.html' ) ; } echo "You Are Now Logged In, Welcome To AdobeTuts!"; } else echo ('<div id="username"> <form action="" method="post"/> <div id="new"> <font color="red"> Wrong Username Or Password, Please Try Again </font> </div> <table><tr><td> <img src="imgs/Log In/username.png" alt=""/> </td><td> <input type="text" size="30" name="username" style="background-color:transparent;" /> </td></tr></table> <table><tr><td> <img src="imgs/Log In/password.png" alt=""/> </td><td> <input type="password" name="password" size="30" /> </td></tr></table> <form id="submitb" action=""> <input type="submit" value="Log in" /> </form> <p class="register">Not yet a member? <a href="Form.html">Register Here</a>, its Free!</p> </div>'); [/code Quote Link to comment https://forums.phpfreaks.com/topic/254481-i-have-created-a-basic-login-using-php-but-how-do-i-link-it-to-another-page/#findComment-1304835 Share on other sites More sharing options...
wolfcry Posted January 6, 2012 Share Posted January 6, 2012 K, I see what your problem is. Here's the fixed code with notes on where I fixed it. if (!($username == " " && $password == " ")){ if ($username == $dbusername && $password == $dbpassword) { header( 'Location: http://adobetuts.netai.net/log in success.html' ) ; } } else { <===== REMOVED EXTRA } and echo statement. echo ('<div id="username"> <form action="" method="post"/> <div id="new"> <font color="red"> Wrong Username Or Password, Please Try Again </font> </div> <table><tr><td> <img src="imgs/Log In/username.png" alt=""/> </td><td> <input type="text" size="30" name="username" style="background-color:transparent;" /> </td></tr></table> <table><tr><td> <img src="imgs/Log In/password.png" alt=""/> </td><td> <input type="password" name="password" size="30" /> </td></tr></table> <form id="submitb" action=""> <input type="submit" value="Log in" /> </form> <p class="register">Not yet a member? <a href="Form.html">Register Here</a>, its Free!</p> </div>'); } UPDATE: Oops, I saw that I forgot the elses' {} braces (which I use, not completely required but keeps code nice and encapsulated which I like.) Quote Link to comment https://forums.phpfreaks.com/topic/254481-i-have-created-a-basic-login-using-php-but-how-do-i-link-it-to-another-page/#findComment-1304837 Share on other sites More sharing options...
usman07 Posted January 6, 2012 Author Share Posted January 6, 2012 im getting a syntax error with the code above? the syntax error says: arse error: syntax error, unexpected T_IS_SMALLER_OR_EQUAL in /home/a3073051/public_html/index.php on line 101 Quote Link to comment https://forums.phpfreaks.com/topic/254481-i-have-created-a-basic-login-using-php-but-how-do-i-link-it-to-another-page/#findComment-1304841 Share on other sites More sharing options...
wolfcry Posted January 6, 2012 Share Posted January 6, 2012 Not sure where that would be coming from unless you have more code I can't see because what I posted is only 35 lines long and that's with spaces between the <?php and ?> tags Quote Link to comment https://forums.phpfreaks.com/topic/254481-i-have-created-a-basic-login-using-php-but-how-do-i-link-it-to-another-page/#findComment-1304843 Share on other sites More sharing options...
usman07 Posted January 6, 2012 Author Share Posted January 6, 2012 yeah sorry i have quite abit of code, showing it all below: <?php $username = $_POST["username"]; $password = $_POST["password"]; //This if statement asks if the $username variable is set. If it is it executes the php script. Otherwise it echoes the login form. if(isset($username)){ if (!($username == " " && $password == " ")) { $connect = mysql_connect("","","") or die("Couldn't connect!"); mysql_select_db("") or die("Couldn't find db"); $query = mysql_query("SELECT * FROM users WHERE username='$username'"); $row = mysql_fetch_array($query); $numrows = mysql_num_rows($query); $dbusername = $row['username']; $dbpassword = $row['password']; } else { echo ('<div id="username"> <form action="" method="post"/> <font color="red"> Please enter a username and password </font> <table><tr><td> <img src="imgs/Log In/username.png" alt=""/> </td><td> <input type="text" size="30" name="username" style="background-color:transparent;" /> </td></tr></table> <table><tr><td> <img src="imgs/Log In/password.png" alt=""/> </td><td> <input type="password" name="password" size="30" /> </td></tr></table> <form id="submitb" action=""> <input type="submit" value="Log in" /> </form> <p class="register">Not yet a member? <a href="Form.html">Register Here</a>, its Free!</p> </div>'); } //check to see if they match if (!($username == " " && $password == " ")){ if ($username == $dbusername && $password == $dbpassword) { header( 'Location: http://adobetuts.netai.net/log in success.html' ) ; } else { echo "Login information incorrect. Please try again."; } } else echo ('<div id="username"> <form action="" method="post"/> <div id="new"> <font color="red"> Wrong Username Or Password, Please Try Again </font> </div> <table><tr><td> <img src="imgs/Log In/username.png" alt=""/> </td><td> <input type="text" size="30" name="username" style="background-color:transparent;" /> </td></tr></table> <table><tr><td> <img src="imgs/Log In/password.png" alt=""/> </td><td> <input type="password" name="password" size="30" /> </td></tr></table> <form id="submitb" action=""> <input type="submit" value="Log in" /> </form> <p class="register">Not yet a member? <a href="Form.html">Register Here</a>, its Free!</p> </div>'); } //This next bit echoes the login form unless the $username variable is set. else { echo ('<div id="username"> <form action="" method="post"/> <table><tr><td> <img src="imgs/Log In/username.png" alt=""/> </td><td> <input type="text" size="30" name="username" style="background-color:transparent;" /> </td></tr></table> <table><tr><td> <img src="imgs/Log In/password.png" alt=""/> </td><td> <input type="password" name="password" size="30" /> </td></tr></table> <form id="submitb" action=""> <input type="submit" value="Log in" /> </form> <p class="register">Not yet a member? <a href="imgs/Homepage tuts/Form.php">Register Here</a>, its Free!</p> </div>'); } ?> Quote Link to comment https://forums.phpfreaks.com/topic/254481-i-have-created-a-basic-login-using-php-but-how-do-i-link-it-to-another-page/#findComment-1304847 Share on other sites More sharing options...
wolfcry Posted January 6, 2012 Share Posted January 6, 2012 Seems you got quite a bit of repetitive code making your work more hard. Why not just use isset() and empty() and evaluate once to see if the submit button has been and the username and password fields aren't empty. That way if submit has NOT been set, then it will show the username and password fields to log in, if it HAS been set, it will evaluate to see if both username and password fields aren't empty and go from there. It'll also be easier to add error messages if submit was set but either the password / username are empty or wrong. Less code to write. here's an example. // Place your SQL query in a different file and call it INTO this page using include_once('url/to/mysqlquery.php') at the very top of your page just below the <?php tag. //Place this at the top of your page or beginning of your PHP form and hide $_POST globals inside to to kill any undefined errors. if(isset($_POST['submit']) && !empty($_POST['username']) && !empty($_POST['password'])){ $username = $_POST['username']; $password = $_POST['password']; if($username == $dbusername && $password == $dbpassword){ header ('url.to.your.page.php'); } else { echo "error message here with form HTML code here."; } } else { echo "form html code here and have form resubmit to self (action = "same_page_url.php")"; } Something like that will be much, much easier to write and easier to maintain. Quote Link to comment https://forums.phpfreaks.com/topic/254481-i-have-created-a-basic-login-using-php-but-how-do-i-link-it-to-another-page/#findComment-1304853 Share on other sites More sharing options...
usman07 Posted January 6, 2012 Author Share Posted January 6, 2012 I appreciate ur help, thing is someone helped me with that php code, i dont reli understand that well, and obviously what u saying is right, but i wont reli be able to do it cuz i wont know where to start or really understand that well and with the time frame i have, i wont have time to try to understand it. Quote Link to comment https://forums.phpfreaks.com/topic/254481-i-have-created-a-basic-login-using-php-but-how-do-i-link-it-to-another-page/#findComment-1304861 Share on other sites More sharing options...
wolfcry Posted January 6, 2012 Share Posted January 6, 2012 I hope you don't mind, but I sent you a PM. Hopefully that will help you out. Right now, I don't have the time to look into your script completely but I'll check it out once I'm free enough to do so. Quote Link to comment https://forums.phpfreaks.com/topic/254481-i-have-created-a-basic-login-using-php-but-how-do-i-link-it-to-another-page/#findComment-1304866 Share on other sites More sharing options...
usman07 Posted January 6, 2012 Author Share Posted January 6, 2012 yeah thats no problem, appreciate it thanks! Quote Link to comment https://forums.phpfreaks.com/topic/254481-i-have-created-a-basic-login-using-php-but-how-do-i-link-it-to-another-page/#findComment-1304870 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.