Jump to content

[SOLVED] go to other page if password is correct


ma5ect

Recommended Posts

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

}

?> 

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');

?> 

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>

<? 

}

?>

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>



<?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>



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>

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

 

 

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>

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>

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>

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.