php - PHP 和 MySQL 中的日志记录/重定向问题

标签 php mysql redirect

使用这段代码我解决了一些问题。当我尝试重定向到子文件夹时,它会忽略注册用户并将我重定向回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();
?>

最佳答案

用以下内容替换 auth.php 中的 header 函数参数 - “静态函数 checkAuthWithRedirect()

static function checkAuthWithRedirect() {
        if(!self::checkAuth()) {
            header('Location:test/admin.php');
        }
    }

这是因为,当您在 auth.php 中的 doLogin() 中将重定向到 test/admin.php 时,

the myAuth::checkAuthWithRedirect(); in admin.php initiates checkAuthWithRedirect() in auth.php where it is again redirecting to index.php

只需将 checkAuthWithRedirect() 中的重定向值替换为 test/admin.php,并将 doLogin() 中的重定向值替换为 test/admin.php。它会起作用的!

关于php - PHP 和 MySQL 中的日志记录/重定向问题,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/37512835/

相关文章:

php - 通过 Bootstrap Modals PHP MySQL 创建搜索记录表单

apache - .htaccess 从 root( public_html ) 重定向到子目录并拒绝直接访问 root

php - 具有不同字段集的记录的数据库设计

php - 使网站登录也适用于 WordPress

php - 如何在 PHP 中转义这个 JavaScript 变量

java - mapView覆盖来自mysql数据库的项目

c# - 使用 MVVM、 Entity Framework 和 mysql 将基本数据绑定(bind)到文本框

.htaccess - 为什么此 RedirectMatch 301 会替换 & 符号?

javascript - 正则表达式捕获尽可能多的 Javascript 重定向

php - 如何管理本地 Shopware 插件 composer.json 依赖项?