使用此代码我已经解决了一些问题。当我尝试重定向到子文件夹时,它会忽略注册用户并将我重定向回index.php站点。
// auth.php
class myAuth {
static function checkAuth() {
// detect user by set cookie
// and value which we saved in session
if(!session_id()) session_start();
// check ...
if(
isset($_COOKIE["auth"])
&&
isset($_SESSION["auth"])
&&
$_COOKIE["auth"] == $_SESSION["auth"]
) {
// extend the session and cookie and in mysql as well
self::_setCookieSessionDBTokenValidity();
return true;
} else {
return false;
}
} // chechAuth finish
// this funkcion redirect user
// on main site (index.php)
// na početnu stranicu (index.php)
// use checkAuthWithRedirect if he's not logged in
static function checkAuthWithRedirect() {
if(!self::checkAuth()) {
header('Location:index.php');
}
} // checkAuthWithRedirect finish
static function doLogin() {
// register user
// save data in session
if(
!empty($_POST['user'])
&&
!empty($_POST['pass'])
) {
if(!session_id()) session_start();
// chech and fetch data for user with sended pass
$user = self::_fetchUserWithPassDB();
// if we find user finish login
if($user) {
// strengthen pass a bit
$token = md5(rand(100000,999999));
// save token in session
$_SESSION["auth"] = $token;
// save user in session
$_SESSION["user"] = $user[0]["user"];
// save role in session
$_SESSION["role"] = $user[0]["role"];
// postavi validity i token u cookie, session i bazu
// save validity and token in cookie, session in db
self::_setCookieSessionDBTokenValidity();
// redirect
header("Location:admin.php");
}
else {
echo '<div class="alert alert-danger" role="alert">';
p('<span class="glyphicon glyphicon-exclamation-sign"></span> USER DOES NOT EXIST!');
echo '</div>';
}
}
} // login
这个代码块在将我重定向到根文件夹中的站点时工作正常,但是当我尝试重定向到子文件夹时,它完全忽略代码并将我重定向到index.php。
示例:
// redirect
header("Location:test/admin.php");
以下是admin.php网站的示例
<?php
// login.php
require_once(__DIR__.'/init.php');
showHTMLHeaderWithTitle('Prijava');
myAuth::checkAuthWithRedirect();
?>
<h1>TEST TEST TEST</h1>
<?php
showHTMLFooter();
?>
答案 0 :(得分:0)
使用以下内容替换头文件函数参数 - “auth.php中的静态函数checkAuthWithRedirect()”
static function checkAuthWithRedirect() {
if(!self::checkAuth()) {
header('Location:test/admin.php');
}
}
这是因为,当你在auth.php中的doLogin()中重定向到test / admin.php时,
admin.php 中的 myAuth :: checkAuthWithRedirect(); 在 auth.php中启动 checkAuthWithRedirect() 它再次重定向到 index.php
只需将 checkAuthWithRedirect()中的重定向值替换为test / admin.php&amp;将 doLogin()中的值重定向到test / admin.php。它会工作!