Jump to content

simple PHP login with redirect problem


odysseusming

Recommended Posts

I'm trying to create a simple PHP login, where the user logs in on the first page (login.php), and if the login is correct, is directed to the second page (page.php). I don't want the user to be able to access the second page without logging in, but I'm having trouble getting the code to work.

 

login.php page code:

 

<?php

 

// Define your username and password

$username = "a";

$password = "b";

 

# define a redirect url

$redirect_url = "http://www.mydomain.com/page.php";

 

if (($_POST['txtUsername'] == $username) && ($_POST['txtPassword'] == $password))

    {

          if (!$_SESSION)

              {

                    session_start();

              }

          $_SESSION['logged_in'] = "yes";

          header("Location: $redirect_url");

          die;

    }

?>

 

<h2>Please Login</h2>

 

<form name="form" method="post" action="<?php echo $_SERVER['PHP_SELF']; ?>">

<p>

<br/>

<label for="txtUsername">Username*</label>

<br/>

  <input name="txtUsername" type="text"  id="txtUsername" size="30" />

  <br/>

<label for="txtPassword">Password*</label>

<br/>

  <input name="txtPassword" type="password"  id="txtPassword" size="30" />

<br/>

<br/>

<input name="Submit" type="submit" value="Login" />

</p>

</form>

 

 

page.php page code:

 

<?php

if ($_SESSION['logged_in'] !== "yes")

    {

          header("Location: http://www.mydomain.com/login.php");

          die;

    }

?>

 

successful

Link to comment
https://forums.phpfreaks.com/topic/144633-simple-php-login-with-redirect-problem/
Share on other sites

<?php
if ($_SESSION['logged_in'] != "yes")  {
          header("Location: http://www.mydomain.com/login.php");
          die;
     }
?>

 

You were using the wrong operator check, use != instead of !== for this scenario and see if that helps you. 

Yeah - I can't figure it out. If I try to first go to the "page.php" page, it redirects me back to the "login.php" as it's supposed to - and then I can login successfully on the first try. But if I go straight to the "login.php" page, I have to login twice.

        if (!$_SESSION)
               {
                     session_start();
               }

 

calling session_start(); at the top of the page without an if is "OK" to do. That is just extra code and is probably causing your issue.

 

Remove the if then move session_start() to the top of your login page.

Still requiring me to login twice. My code now looks like this:

 

login.php page code:

 

<?php

session_start();

 

// Define your username and password

$username = "a";

$password = "b";

 

# define a redirect url

$redirect_url = "http://www.mydomain.com/page.php";

 

if (($_POST['txtUsername'] == $username) && ($_POST['txtPassword'] == $password))

    {

          $_SESSION['logged_in'] = "yes";

          header("Location: $redirect_url");

          die;

    }

?>

 

<h2>Please Login</h2>

 

<form name="form" method="post" action="<?php echo $_SERVER['PHP_SELF']; ?>">

<p>

<br/>

<label for="txtUsername">Username*</label>

<br/>

  <input name="txtUsername" type="text"  id="txtUsername" size="30" />

  <br/>

<label for="txtPassword">Password*</label>

<br/>

  <input name="txtPassword" type="password"  id="txtPassword" size="30" />

<br/>

<br/>

<input name="Submit" type="submit" value="Login" />

</p>

</form>

 

 

page.php page code:

 

<?php

session_start();

if ($_SESSION['logged_in'] !== "yes")

    {

          header("Location: http://www.mydomain.com/login.php");

          die;

    }

?>

 

successful

Please use [ code] [ /code] tags for posting code in (no initial space).

 

<?php
session_start();

// Define your username and password
$username = "a";
$password = "b";

# define a redirect url
$redirect_url = "http://www.mydomain.com/page.php";

if (isset($_SESSION['logged_in']) && $_SESSION['logged_in'] == "yes") {
          header("Location: $redirect_url");
}

if (($_POST['txtUsername'] == $username) && ($_POST['txtPassword'] == $password)) {
          $_SESSION['logged_in'] = "yes";
          header("Location: $redirect_url");
          die;
     }
?>

 

A little redundant, but that should work to avoid logging in twice.

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.