Page 1 of 1
too many redirects
Posted: Sun Jan 06, 2013 7:43 pm
by Porkypie
Hey,
is there an easy way too find out why my php page keeps looping?
I've tried to clear my cookies already because the last time it happened it was because of that.
Also it's quite weird it's happening because the first time I tried my page I just received a php error.
I fixed it but then the "too many redirects" message started. I tried to make it go away by actually putting the
php error back into the code but it didn't matter.
thanks for your time and help.
Porkypie
Re: too many redirects
Posted: Mon Jan 07, 2013 12:07 am
by ExtremeGaming
Make sure you put a:
die;
below every header redirect.
This
might also be some htaccess problem? There really is no definite answer until we can see something.
Re: too many redirects
Posted: Tue Jan 08, 2013 10:13 am
by Porkypie
I guess the error should be in this code. Every page gives the redirection error and this one is included in each one of them:
<?php
session_start();
$exceptions = array('register', 'login');
$page = substr(end(explode('/', $_SERVER['script_name'])), 0, -4);
if (in_array($page, $exceptions) === false){
if (isset($_SESSION['username']) === false){
header('location: login.php');
die();
}
}
mysql_connect('**********', '**********', '**********');
mysql_select_db('user_system');
$path = dirname(__FILE__);
include("{$path}/inc/user.inc.php");
?
>
The user.inc.php file only contains functions without any redirects so I guess that isn't the problem.
Also every page redirects to the following code as long as there are no errors:
<?php include('core/init.inc.php'); ?>
<html>
<head>
<title></title>
</head>
<body>
<p>
Je bent nu ingelogt als <?php echo $_SESSION['username']; ?>
</p>
<p>
<a href="logout.php">Logout</a>
</p>
</body>
</html>
The only header redirect without a "die;" after it is the logout page. But putting it there doesn't matter.
Re: too many redirects
Posted: Tue Jan 08, 2013 9:30 pm
by Helx
"isset();" checks if the variable "is set". I don't think it can be checked against false the way you are doing it.
Instead of:
if (isset($_SESSION['username']) === false){
header('location: login.php');
die();
}
Try:
if (!isset($_SESSION['username'])){
header('location: login.php');
die();
}
Note the exclamation mark in front of "isset". Try this with in_array as well.
Re: too many redirects
Posted: Tue Jan 08, 2013 11:16 pm
by Porkypie
I've tried out what you've said but it didn't work.
I had another look at the tutorial and found out what i've done wrong.
The script_name has to be written in caps...
$page = substr(end(explode('/', $_SERVER['script_name'])), 0, -4);
thanks for your help
Re: too many redirects
Posted: Thu Jan 10, 2013 7:47 pm
by jacek
Displaying E_NOTICE level errors will help spot this kind of thing, It would have given a message telling you that there was no such variable