php eregi() problem
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..
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.
Try:
if (!isset($_SESSION('username'))) {
if(FALSE === strpos($_SERVER['PHP_SELF'], 'login.php')) {
header('Location: login.php');
exit();
}
}
Some Suggestions:
- Make sure that you have
session_start()
placed on top of your script - Use
isset($_SESSION.......
- Use
strpos
instead of deprecatederegi
- Don't use deprecated
session_is_registered
- Use
exit/die
after header redirect - It is
$_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);
精彩评论