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

}

?> 

Link to comment
Share on other sites

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
Share on other sites

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
Share on other sites

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
Share on other sites

<?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
Share on other sites

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
Share on other sites

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
Share on other sites

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
Share on other sites

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
Share on other sites

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
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • 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.