/    Sign up×
Community /Pin to ProfileBookmark

[RESOLVED] Redirect loop error :(

Hey

I am having a small problem with my login page. It first checks if there is a session IE if logged in, if not to goes to the login page, if it is logged in then it will remain on the main page.

[code=php]
<?php session_start();

if (!isset($_SESSION[‘isLogged’])) {

header(“Location: ./../_admin/index.php?function=Login”);
exit();

} else {

include ‘./../_admin/includes/header.php’;
?>

<table align=”center” width=”996″ border=”1″ class=”content” cellpadding=”4″ cellspacing=”4″><tr><td>
<table align=”center” width=”985″ border=”0″ cellpadding=”4″ cellspacing=”4″>
<tr>
<td align=”center” valign=”top”>
<?php
if (isset($_SESSION[‘status’])) {
echo $_SESSION[‘status’];
} else {
echo ‘Welcome ‘.$_SESSION[‘username’].’ – Use the links above to navigate!’;
}
?>
</td>
</tr>
</table>
</td></tr></table>

<?php
}

if ($_GET[‘function’] == “Login”) {
$_SESSION[‘isLogged’] = ”;

include ‘./../_admin/includes/header.php’;
?>

<table align=”center” width=”996″ border=”1″ class=”content” cellpadding=”4″ cellspacing=”4″><tr><td>
<table align=”center” width=”985″ border=”0″ cellpadding=”4″ cellspacing=”4″>
<tr>
<td align=”center” valign=”top”>
<form action=”index.php?function=LoginDo” method=”post” name=”login” id=”login”>
<table width=”100%” border=”0″ align=”center” cellpadding=”5″ cellspacing=”0″>
<tr>
<td colspan=”2″><div align=”center” style=”color:#FF0000″><strong><?php echo $_SESSION[‘message’]; ?></strong></div></td>
</tr>
<tr>
<td colspan=”2″><div align=”center”><strong>Please log in:</strong></div></td>
</tr>
<tr>
<td width=”47%”><strong>Username:</strong></td>
<td width=”53%”><input name=”username” type=”text” id=”username”></td>
</tr>
<tr>
<td><strong>Password:</strong></td>
<td><input name=”password” type=”password” id=”password”></td>
</tr>
<tr>
<td colspan=”2″><div align=”center”>
<input name=”Submit” type=”submit” id=”Submit” value=”Sign In”>
<input name=”submitID” type=”hidden” id=”submitID” value=”1″><br /><br />
<a href=”index.php?function=Forgot”>Forgot Password?</a></div></td>
</tr>
</table>
</form>
</td>
</tr>
</table>
</td></tr></table>

<?php

} elseif ($_GET[‘function’] == “LoginDo”) {
if($_POST[‘submitID’] == ‘1’){

$username = mysql_real_escape_string($_POST[‘username’]);
$password = mysql_real_escape_string($_POST[‘password’]);

$_SESSION[‘message’] = NULL;

if($username == NULL) {
$_SESSION[‘message’] = ‘Please enter username.’;
header(“Location: ./../_admin/index.php?function=Login”);
exit();
}

if($_SESSION[‘message’] == NULL && $password == NULL){
$_SESSION[‘message’] = ‘Please enter password.’;
header(“Location: ./../_admin/index.php?function=Login”);
exit();
}

if($_SESSION[‘message’] == NULL)
{
$userQuery = mysql_fetch_row(mysql_query(“SELECT COUNT(*) FROM ebcs_settings WHERE `Settings_Username`=’$username’ AND `Settings_Password`=’$password'”));

if($userQuery[0] > 0){

$_SESSION[‘message’] = NULL;
$_SESSION[‘isLogged’] = ‘1’;
$_SESSION[‘username’] = $username;
setcookie(“isLogged”, ‘1’, time()+86400);
setcookie(“username”, $username, time()+86400);
header(“Location: ./../_admin/index.php”);
exit();

} else {

$_SESSION[‘message’] = ‘Invalid username and/or password!’;
header(“Location: ./../_admin/index.php?function=Login”);
exit();
}
}

} else {
header(“Location: ./../_admin/index.php?function=Login”);
exit();
}

}
?>
[/code]

Thanks for any help!

to post a comment
PHP

6 Comments(s)

Copy linkTweet thisAlerts:
@talmanJun 17.2011 — I have never seen relative URLs used with header Location before, try using the absolute URL.
Copy linkTweet thisAlerts:
@LiL_aaronauthorJun 17.2011 — I have tried using

domain.com/foldier/_admin/index.php?function=Login

And i still get the "This web page has a redirect loop" error.
Copy linkTweet thisAlerts:
@NogDogJun 17.2011 — Is it trying to redirect to itself? That would generate an error like that since it would be an infinite loop of redirections as long as $_SESSION['isLogged'] is empty. You need to change the sequence of checks, I think:

[code=php]
<?php
if(!empty($_GET['function']) {
if($_GET['function'] == 'Login') {
// display the login form
}
elseif($_GET['function'] == 'LoginDo') {
// process the login
}
}
elseif(empty($_SESSION['isLogged'])) {
$host = $_SERVER['HTTP_HOST'];
$uri = rtrim(dirname($_SERVER['PHP_SELF']), '/\');
$url = 'http://$host$uri?function=Login';
header("Location: $url");
exit;
}
// rest of page
[/code]


However, I'd recommend not redirecting at all. Instead, use conditional includes and/or function calls to handle the different cases. I like to put all the login stuff into an include file, which handles displaying the login form if needed (which might be a second include file called from the login include file), processing login requests, setting the session value if successfully logged in, etc. Then on the main page all you need is:
[code=php]
<?php
require $_SERVER['DOCUMENT_ROOT'].'/includes/login.php';
?><html>...rest of page...</html>
[/code]
Copy linkTweet thisAlerts:
@talmanJun 17.2011 — Did you include "http://"?

Should look like this:
[code=php]header("Location: http://domain.com/foldier/_admin/index.php?function=Login");[/code]
Copy linkTweet thisAlerts:
@LiL_aaronauthorJun 17.2011 — Hey thanks for the help guys!

I did fix it this way tho, but i will look into nogdogs recommendation

This is the working code.

[code=php]
<?php session_start();

if ($_GET['function'] == "Dashboard") {
if (isset($_SESSION['isLogged'])) {

include './../_admin/includes/header.php';

?>

<table align="center" width="996" border="1" class="content" cellpadding="4" cellspacing="4"><tr><td>
<table align="center" width="985" border="0" cellpadding="4" cellspacing="4">
<tr>
<td align="center" valign="top">
<?php
if (isset($_SESSION['status'])) {
echo $_SESSION['status'];
} else {
echo 'Welcome '.$_SESSION['username'].' - Use the links above to navigate!';
}
?>
</td>
</tr>
</table>
</td></tr></table>

<?php

} else {
header("Location: ./../_admin/index.php?function=Login");
exit();
}

} elseif ($_GET['function'] == "Login") {
$_SESSION['isLogged'] = '';

include './../_admin/includes/header.php';

?>

<table align="center" width="996" border="1" class="content" cellpadding="4" cellspacing="4"><tr><td>
<table align="center" width="985" border="0" cellpadding="4" cellspacing="4">
<tr>
<td align="center" valign="top">
<form action="index.php?function=LoginDo" method="post" name="login" id="login">
<table width="100&#37;" border="0" align="center" cellpadding="5" cellspacing="0">
<tr>
<td colspan="2"><div align="center" style="color:#FF0000"><strong><?php echo $_SESSION['message']; ?></strong></div></td>
</tr>
<tr>
<td colspan="2"><div align="center"><strong>Please log in:</strong></div></td>
</tr>
<tr>
<td width="47%"><strong>Username:</strong></td>
<td width="53%"><input name="adminuser" type="text" id="username"></td>
</tr>
<tr>
<td><strong>Password:</strong></td>
<td><input name="password" type="password" id="password"></td>
</tr>
<tr>
<td colspan="2"><div align="center">
<input name="Submit" type="submit" id="Submit" value="Sign In">
<input name="submitID" type="hidden" id="submitID" value="1"><br /><br />
<a href="index.php?function=Forgot">Forgot Password?</a></div></td>
</tr>
</table>
</form>
</td>
</tr>
</table>
</td></tr></table>

<?php

} elseif ($_GET['function'] == "LoginDo") {
if($_POST['submitID'] == '1'){

include './../_admin/includes/dbconnect.php';
$adminuser = mysql_real_escape_string($_POST['adminuser']);
$password = mysql_real_escape_string($_POST['password']);

$_SESSION['message'] = NULL;

if($adminuser == NULL) {
$_SESSION['message'] = 'Please enter username.';
header("Location: ./../_admin/index.php?function=Login");
exit();
}

if($_SESSION['message'] == NULL && $password == NULL){
$_SESSION['message'] = 'Please enter password.';
header("Location: ./../_admin/index.php?function=Login");
exit();
}

if($_SESSION['message'] == NULL) {
$userQuery = mysql_fetch_row(mysql_query("SELECT COUNT(*) FROM ebcs_settings WHERE Settings_Username='$adminuser' AND Settings_Password='$password'"));

if($userQuery[0] > 0){

$_SESSION['message'] = NULL;
$_SESSION['isLogged'] = '1';
$_SESSION['username'] = $adminuser;
setcookie("isLogged", '1', time()+86400);
setcookie("username", $adminuser, time()+86400);
header("Location: ./../_admin/index.php?function=Dashboard");
exit();

} else {

$_SESSION['message'] = 'Invalid username and/or password!';
header("Location: ./../_admin/index.php?function=Login");
exit();
}
}

} else {
header("Location: ./../_admin/index.php?function=Login");
exit();
}
?>
[/code]
Copy linkTweet thisAlerts:
@LiL_aaronauthorJun 17.2011 — I do have another issue tho, but i will post in another thread! ?
×

Success!

Help @LiL_aaron spread the word by sharing this article on Twitter...

Tweet This
Sign in
Forgot password?
Sign in with TwitchSign in with GithubCreate Account
about: ({
version: 0.1.9 BETA 5.18,
whats_new: community page,
up_next: more Davinci•003 tasks,
coming_soon: events calendar,
social: @webDeveloperHQ
});

legal: ({
terms: of use,
privacy: policy
});
changelog: (
version: 0.1.9,
notes: added community page

version: 0.1.8,
notes: added Davinci•003

version: 0.1.7,
notes: upvote answers to bounties

version: 0.1.6,
notes: article editor refresh
)...
recent_tips: (
tipper: @AriseFacilitySolutions09,
tipped: article
amount: 1000 SATS,

tipper: @Yussuf4331,
tipped: article
amount: 1000 SATS,

tipper: @darkwebsites540,
tipped: article
amount: 10 SATS,
)...