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 } ?> Quote Link to comment 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 Quote Link to comment 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'); ?> Quote Link to comment 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. Quote Link to comment 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> <? } ?> Quote Link to comment 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. Quote Link to comment 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> Quote Link to comment 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 Quote Link to comment 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 Quote Link to comment 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> Quote Link to comment 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. Quote Link to comment 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. Quote Link to comment 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> Quote Link to comment 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 Quote Link to comment 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> Quote Link to comment 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? Quote Link to comment 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> Quote Link to comment 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> Quote Link to comment 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 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.