too many redirects

Ask about a PHP problem here.
Post Reply
Porkypie
Posts: 25
Joined: Wed Jan 02, 2013 3:52 pm
Location: Netherlands

too many redirects

Post 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
ExtremeGaming
Posts: 205
Joined: Mon Jul 09, 2012 11:13 pm

Re: too many redirects

Post by ExtremeGaming »

Make sure you put a:
[syntax=php]die;[/syntax]
below every header redirect.

This might also be some htaccess problem? There really is no definite answer until we can see something.
<?php while(!$succeed = try()); ?>
Porkypie
Posts: 25
Joined: Wed Jan 02, 2013 3:52 pm
Location: Netherlands

Re: too many redirects

Post 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:

[syntax=php]
<?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");

?
>[/syntax]

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:

[syntax=php]<?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>[/syntax]

The only header redirect without a "die;" after it is the logout page. But putting it there doesn't matter.
User avatar
Helx
Posts: 350
Joined: Thu May 17, 2012 6:45 am
Location: Auckland, New Zealand

Re: too many redirects

Post 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:
[syntax=php]if (isset($_SESSION['username']) === false){
header('location: login.php');
die();
}[/syntax]

Try:
[syntax=php]if (!isset($_SESSION['username'])){
header('location: login.php');
die();
}[/syntax]

Note the exclamation mark in front of "isset". Try this with in_array as well.
Porkypie
Posts: 25
Joined: Wed Jan 02, 2013 3:52 pm
Location: Netherlands

Re: too many redirects

Post 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...
[syntax=php]
$page = substr(end(explode('/', $_SERVER['script_name'])), 0, -4);
[/syntax]

thanks for your help ;)
User avatar
jacek
Site Admin
Posts: 3262
Joined: Thu May 05, 2011 1:45 pm
Location: UK
Contact:

Re: too many redirects

Post 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 :)
Image
Post Reply