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

Link to comment
Share on other sites

        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.

Link to comment
Share on other sites

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

Link to comment
Share on other sites

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.

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.