oliverbj
oliverbj

Reputation: 6052

PHP - Redirect only once

How can I make the header("Location: /path"); to only redirect once?

Currenlty I have this:

//If account is locked by user.
if($userdata['account_locked']  == 1):
    header("Location: /account/locked");
endif;

This script is checked in my header file. But it doesn't work, since it will just create a loop-redirect (infinit loop)

How can I do, so it only redirect once?

Thanks in advance.

Upvotes: 0

Views: 5977

Answers (2)

dAm2K
dAm2K

Reputation: 10339

This is simple, but it's a bit ugly...

//If account is locked by user.
if($userdata['account_locked']  == 1):
    if ($_SERVER['REQUEST_URI'] != "/account/locked") header("Location: /account/locked");
endif;

You should use MVC, a controller should be aware of the loop.

EDITED It's better to use

$_SERVER['PHP_SELF']

instead of

$_SERVER['REQUEST_URI'].

Edit: Changed from URL to URI.

Upvotes: 1

John Skoubourdis
John Skoubourdis

Reputation: 3259

if($userdata['account_locked']  == 1 && $_SERVER['HTTP_REFERER'] != 'http://'.$_SERVER['HTTP_HOST'].'/account/locked'):
    header("Location: /account/locked");
endif;

Upvotes: 0

Related Questions