Jump to content

Recommended Posts

Hi all, I have an admin area which works using sessions, should someone try and go directly to a specific page (e.g. postinfo.php) in the admin area the code should redirect them to the login page (adminloginstart.php) , if the username and password they enter are correct the login page should then redirect them to the original page they requested.

The code works fine apart from not redirecting the valid user back to the original requested URL

all off the codes in question are listed below, can anyone please help?

 

postinfo.php:-

<?

require_once('watertonloginc.php');

?>

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">

<html xmlns="http://www.w3.org/1999/xhtml">

 

<?

require_once('connectionlocal.php');

?>

------------------------------------------------------------

watertonloginc.php:-

<?

session_start();

if ($_SESSION['logged'] != 1)

{

$redirect = $_SERVER['PHP_SELF'];

header("Location: adminloginstart.php?redirect=$redirect");

}

?>

-------------------------------------------------------------

adminloginstart.php:-

<?php

session_start();

ob_start();

session_register('logged'); // add single quotes to avoid the constant stuff.

$_SESSION['logged'] = 0;

?><!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"

"http://www.w3.org/TR/html4/loose.dtd">

<?

$redirect = isset($_POST['redirect'])?$_POST['redirect']:null; // added a check here to avoid the index error. also changed to POST.

if (isset($_POST['submit']))

{

  if ($_POST['username'] == "Paul" && $_POST['password'] == "daleragu")// replace this logic with authorization from mysql if needed

  {//if it gets to this point, the authorization is correct, the we can redirect with the header 3 lines down

  $redirect = $_POST['redirect'];

  $_SESSION['logged'] = 1;

  header ("Location: $redirect");

  }

  else

  {

  ?>

<head>

<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1" />

<title>Untitled Document</title>

</head>

 

<body>

<table width="502" border="0" align="center" cellpadding="5" cellspacing="0" class="h3">

Invalid Username and/or Password

 

 

    <form action="adminloginstart.php" method="post">

    <input type="hidden" name="redirect" value="<? echo $redirect; ?>">

    Username: <input type="text" name="username">

 

    Password: <input type="password" name="password">

 

 

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

    </form>

            </table>

    <?

  }

}

else

{

?>

<table width="502" border="0" align="center" cellpadding="5" cellspacing="0" class="h3">

Invalid Username and/or Password

 

 

    <form action="adminloginstart.php" method="post">

    <input type="hidden" name="redirect" value="<? echo $redirect; ?>">

    Username: <input type="text" name="username">

 

    Password: <input type="password" name="password">

 

 

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

    </form>

            </table>

<?

}

?>

Link to comment
https://forums.phpfreaks.com/topic/44700-authenication-redirect-not-working/
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.