ma5ect Posted November 22, 2008 Share Posted November 22, 2008 Hi all, I have the following code..the username and password works fine but i want the script to go to another webpage if the username and password is enetered correctly. how do i code the if statement?? <?php $username = "someuser"; $password = "somepassword"; if ($_POST['txtUsername'] != $username || $_POST['txtPassword'] != $password) { ?> <h1>Login</h1> <form name="form" method="post" action="<?php echo $_SERVER['PHP_SELF']; ?>"> <p><label for="txtUsername">Username:</label> <br /><input type="text" title="Enter your Username" name="txtUsername" /></p> <p><label for="txtpassword">Password:</label> <br /><input type="password" title="Enter your password" name="txtPassword" /></p> <p><input type="submit" name="Submit" value="Login" /></p> </form> <?php } else { ?> <p>This is the protected page. Your private content goes here.</p> <?php } ?> Link to comment https://forums.phpfreaks.com/topic/133790-solved-go-to-other-page-if-password-is-correct/ Share on other sites More sharing options...
gevans Posted November 22, 2008 Share Posted November 22, 2008 header('Location: yourpage.php'); That will forward you to a new page Link to comment https://forums.phpfreaks.com/topic/133790-solved-go-to-other-page-if-password-is-correct/#findComment-696272 Share on other sites More sharing options...
ma5ect Posted November 22, 2008 Author Share Posted November 22, 2008 i get the follwoing error messge: Warning: Cannot modify header information - headers already sent by (output started at C:\xampp\htdocs\test.php:12) in C:\xampp\htdocs\test.php on line 33 code: <?php // Define your username and password $username = "someuser"; $password = "somepassword"; if ($_POST['txtUsername'] != $username || $_POST['txtPassword'] != $password) { ?> <h1>Login</h1> <form name="form" method="post" action="<?php echo $_SERVER['PHP_SELF']; ?>"> <p><label for="txtUsername">Username:</label> <br /><input type="text" title="Enter your Username" name="txtUsername" /></p> <p><label for="txtpassword">Password:</label> <br /><input type="password" title="Enter your password" name="txtPassword" /></p> <p><input type="submit" name="Submit" value="Login" /></p> </form> <?php } else { ?> <?php }header('Location: test.php'); ?> Link to comment https://forums.phpfreaks.com/topic/133790-solved-go-to-other-page-if-password-is-correct/#findComment-696287 Share on other sites More sharing options...
samona Posted November 22, 2008 Share Posted November 22, 2008 You can't output data before the header() function. Link to comment https://forums.phpfreaks.com/topic/133790-solved-go-to-other-page-if-password-is-correct/#findComment-696306 Share on other sites More sharing options...
samona Posted November 22, 2008 Share Posted November 22, 2008 Try the code below and let me know if it works. <?php // Define your username and password $username = "someuser"; $password = "somepassword"; if ($_POST['txtUsername'] != $username || $_POST['txtPassword'] != $password) { header("Location:page_to_display_error"); else { ?> <h1>Login</h1> <form name="form" method="post" action="<?php echo $_SERVER['PHP_SELF']; ?>"> <p><label for="txtUsername">Username:</label> <br /><input type="text" title="Enter your Username" name="txtUsername" /></p> <p><label for="txtpassword">Password:</label> <br /><input type="password" title="Enter your password" name="txtPassword" /></p> <p><input type="submit" name="Submit" value="Login" /></p> </form> <? } ?> Link to comment https://forums.phpfreaks.com/topic/133790-solved-go-to-other-page-if-password-is-correct/#findComment-696307 Share on other sites More sharing options...
samona Posted November 22, 2008 Share Posted November 22, 2008 Oh, you want to go to another page if the password is correct. Sorry! Ummm give me a few minutes. Link to comment https://forums.phpfreaks.com/topic/133790-solved-go-to-other-page-if-password-is-correct/#findComment-696310 Share on other sites More sharing options...
samona Posted November 22, 2008 Share Posted November 22, 2008 Try the following <?php if (isset($_POST['submit']) { // Define your username and password $post_username = $_POST['txtUsername']; $post_password = $_POST['txtPassword']; $username = "someuser"; $password = "somepassword"; if ($post_username == $username || $post_password == $password) { header("Location:next_page"); } else { header("Location:error_page"); } } //If the user hasn't pressed the submit button then display the form <h1>Login</h1> <form name="form" method="post" action="<?php echo $_SERVER['PHP_SELF']; ?>"> <p><label for="txtUsername">Username:</label> <br /><input type="text" title="Enter your Username" name="txtUsername" /></p> <p><label for="txtpassword">Password:</label> <br /><input type="password" title="Enter your password" name="txtPassword" /></p> <p><input type="submit" name="Submit" value="Login" /></p> </form> Link to comment https://forums.phpfreaks.com/topic/133790-solved-go-to-other-page-if-password-is-correct/#findComment-696318 Share on other sites More sharing options...
ma5ect Posted November 22, 2008 Author Share Posted November 22, 2008 Parse error: syntax error, unexpected '{' in C:\xampp\htdocs\test.php on line 4 Link to comment https://forums.phpfreaks.com/topic/133790-solved-go-to-other-page-if-password-is-correct/#findComment-696320 Share on other sites More sharing options...
samona Posted November 22, 2008 Share Posted November 22, 2008 Sorry, I forgot a paranthesis. Change it to this if (isset($_POST['submit'])) { // Define your username and password Link to comment https://forums.phpfreaks.com/topic/133790-solved-go-to-other-page-if-password-is-correct/#findComment-696321 Share on other sites More sharing options...
dezkit Posted November 22, 2008 Share Posted November 22, 2008 <?php if (isset($_POST['submit'])){ $post_username = $_POST['txtUsername']; $post_password = $_POST['txtPassword']; $username = "someuser"; $password = "somepassword"; if ($post_username == $username || $post_password == $password) { header("Location:next_page"); } else { header("Location:error_page"); } } ?> <h1>Login</h1> <form name="form" method="post" action="<?php echo $_SERVER['PHP_SELF']; ?>"> <p><label for="txtUsername">Username:</label> <br /><input type="text" title="Enter your Username" name="txtUsername" /></p> <p><label for="txtpassword">Password:</label> <br /><input type="password" title="Enter your password" name="txtPassword" /></p> <p><input type="submit" name="Submit" value="Login" /></p> </form> Link to comment https://forums.phpfreaks.com/topic/133790-solved-go-to-other-page-if-password-is-correct/#findComment-696323 Share on other sites More sharing options...
dezkit Posted November 22, 2008 Share Posted November 22, 2008 I recommend using sessions if you want the next_page to be private unless you are logged in, if you want sessions, reply. Link to comment https://forums.phpfreaks.com/topic/133790-solved-go-to-other-page-if-password-is-correct/#findComment-696326 Share on other sites More sharing options...
samona Posted November 22, 2008 Share Posted November 22, 2008 I agree with deskit. If you want a login system than sessions are the way to go. Link to comment https://forums.phpfreaks.com/topic/133790-solved-go-to-other-page-if-password-is-correct/#findComment-696327 Share on other sites More sharing options...
ma5ect Posted November 22, 2008 Author Share Posted November 22, 2008 sorry folks, the code enters the username and password but does not send the user to the output pages...keeps displaying the login page <?php if (isset($_POST['submit'])) { // Define your username and password $post_username = $_POST['txtUsername']; $post_password = $_POST['txtPassword']; $username = "test"; $password = "test"; if ($post_username == $username || $post_password == $password) { header("Location: mainlogin.php"); } else { header("Location: mainlogin.php"); } } //If the user hasn't pressed the submit button then display the form ?> <h1>Login</h1> <form name="form" method="post" action="<?php echo $_SERVER['PHP_SELF']; ?>"> <p><label for="txtUsername">Username:</label> <br /><input type="text" title="Enter your Username" name="txtUsername" /></p> <p><label for="txtpassword">Password:</label> <br /><input type="password" title="Enter your password" name="txtPassword" /></p> <p><input type="submit" name="Submit" value="Login" /></p> </form> Link to comment https://forums.phpfreaks.com/topic/133790-solved-go-to-other-page-if-password-is-correct/#findComment-696335 Share on other sites More sharing options...
samona Posted November 22, 2008 Share Posted November 22, 2008 For example <?php if (isset($_POST['submit']) { // Define your username and password $post_username = $_POST['txtUsername']; $post_password = $_POST['txtPassword']; $username = "someuser"; $password = "somepassword"; if ($post_username == $username && $post_password == $password) { session_start(); $_Session["login_name"] = $username; header("Location:next_page"); } else { header("Location:error_page"); } } //If the user hasn't pressed the submit button then display the form <h1>Login</h1> <form name="form" method="post" action="<?php echo $_SERVER['PHP_SELF']; ?>"> <p><label for="txtUsername">Username:</label> <br /><input type="text" title="Enter your Username" name="txtUsername" /></p> <p><label for="txtpassword">Password:</label> <br /><input type="password" title="Enter your password" name="txtPassword" /></p> <p><input type="submit" name="Submit" value="Login" /></p> </form> Then the next page would be <?php session_start(); if (!isset($_SESSION["login_name"])) { //revert them to error page if they have not logged in. header("Location:error_page.php); } //Contine with rest of your code Link to comment https://forums.phpfreaks.com/topic/133790-solved-go-to-other-page-if-password-is-correct/#findComment-696338 Share on other sites More sharing options...
samona Posted November 22, 2008 Share Posted November 22, 2008 Try the code below. I changed the if statement to AND instead of OR <?php if (isset($_POST['submit'])) { // Define your username and password $post_username = $_POST['txtUsername']; $post_password = $_POST['txtPassword']; $username = "test"; $password = "test"; if ($post_username == $username && $post_password == $password) { header("Location: mainlogin.php"); } else { header("Location: mainlogin.php"); } } //If the user hasn't pressed the submit button then display the form ?> <h1>Login</h1> <form name="form" method="post" action="<?php echo $_SERVER['PHP_SELF']; ?>"> <p><label for="txtUsername">Username:</label> <br /><input type="text" title="Enter your Username" name="txtUsername" /></p> <p><label for="txtpassword">Password:</label> <br /><input type="password" title="Enter your password" name="txtPassword" /></p> <p><input type="submit" name="Submit" value="Login" /></p> </form> Link to comment https://forums.phpfreaks.com/topic/133790-solved-go-to-other-page-if-password-is-correct/#findComment-696343 Share on other sites More sharing options...
samona Posted November 22, 2008 Share Posted November 22, 2008 By the way. What are the name of your pages? Link to comment https://forums.phpfreaks.com/topic/133790-solved-go-to-other-page-if-password-is-correct/#findComment-696348 Share on other sites More sharing options...
ma5ect Posted November 22, 2008 Author Share Posted November 22, 2008 i prefer not to use sessions...the alter of the of statement still outputs the same result.. <?php if (isset($_POST['submit'])) { // Define your username and password $post_username = $_POST['txtUsername']; $post_password = $_POST['txtPassword']; $username = "test"; $password = "admin"; if ($post_username == $username && $post_password == $password) { header("Location: welcome.php"); } else { header("Location: error.php"); } } //If the user hasn't pressed the submit button then display the form ?> <h1>Login</h1> <form name="form" method="post" action="<?php echo $_SERVER['PHP_SELF']; ?>"> <p><label for="txtUsername">Username:</label> <br /><input type="text" title="Enter your Username" name="txtUsername" /></p> <p><label for="txtpassword">Password:</label> <br /><input type="password" title="Enter your password" name="txtPassword" /></p> <p><input type="submit" name="Submit" value="Login" /></p> </form> Link to comment https://forums.phpfreaks.com/topic/133790-solved-go-to-other-page-if-password-is-correct/#findComment-696352 Share on other sites More sharing options...
samona Posted November 22, 2008 Share Posted November 22, 2008 OK, I just put this on my server and ran it. Should work now. The problem was the name for the submit button you had was "Submit". I changed the form so the name of the submit button is "submit" and I included an else statement. Actually, you dont need the else statement. I'll revise it... <?php if (isset($_POST['submit'])) { // Define your username and password $post_username = trim($_POST['txtUsername']); $post_password = trim($_POST['txtPassword']); $username = "test"; $password = "admin"; if ($post_username == $username && $post_password == $password) { header("Location: welcome.php"); } else { header("Location: error.php"); } } //If the user hasn't pressed the submit button then display the form ?> <h1>Login</h1> <form name="form" method="post" action="<?php echo $_SERVER['PHP_SELF']; ?>"> <p><label for="txtUsername">Username:</label> <br /><input type="text" title="Enter your Username" name="txtUsername" /></p> <p><label for="txtpassword">Password:</label> <br /><input type="password" title="Enter your password" name="txtPassword" /></p> <p><input type="submit" name="submit" value="Login" /></p> </form> Link to comment https://forums.phpfreaks.com/topic/133790-solved-go-to-other-page-if-password-is-correct/#findComment-696370 Share on other sites More sharing options...
ma5ect Posted November 22, 2008 Author Share Posted November 22, 2008 ah yes just realised.. genius.. much appreciated Link to comment https://forums.phpfreaks.com/topic/133790-solved-go-to-other-page-if-password-is-correct/#findComment-696373 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.