Reputation: 1062
I have this problem with my code, the page is redirecting and wouldn't stop.. The browser stop it because it's redirecting all the time, and I have a global.php file that included in all php pages, so I putted this code for the session and it went like what I said
if (!session_is_registered('username')) {
if(!eregi('login.php', $PHP_SELF)) header('Location: login.php');
}
Also global.php included in login.php, but when I start it on the web server of my site, it does what i said before, but on my based server on computer it works fine, so please help me fast
and sorry for my english..
Upvotes: 0
Views: 256
Reputation: 8382
Try this:
session_start();
if (!isset($_SESSION['username']) && stripos($_SERVER['PHP_SELF'], 'login.php') === false) {
header('Location: login.php');
}
Both session_is_registered()
and eregi()
are deprecated functions and shouldn't be used. Plus, regular expressions are overkill for what you're doing anyways.
Upvotes: 2
Reputation: 382696
Try:
if (!isset($_SESSION('username'))) {
if(FALSE === strpos($_SERVER['PHP_SELF'], 'login.php')) {
header('Location: login.php');
exit();
}
}
Some Suggestions:
session_start()
placed on top of your scriptisset($_SESSION.......
strpos
instead of deprecated eregi
session_is_registered
exit/die
after header redirect$_SERVER['PHP_SELF']
not $PHP_SELF
Use error handling, put these lines on top of your script:
ini_set('display_errors', true);
error_reporting(E_ALL);
Upvotes: 0