doulan4371 2016-05-29 17:24
浏览 63

在PHP和MySQL中记录/重定向的问题

using this code I've countered a bit of a problem. When I try to redirect to subfolder it ignores registered user and redirects me back to index.php site.

// 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

This block of code works fine when redirect me to site which is in root folder but when I try to redirect to subfolder it completily ignores code and redirects me to index.php.

Example:

            // redirect 
            header("Location:test/admin.php");

And here is example of admin.php site

<?php
// login.php
require_once(__DIR__.'/init.php');
showHTMLHeaderWithTitle('Prijava');
myAuth::checkAuthWithRedirect();
?>


<h1>TEST TEST TEST</h1>



<?php
showHTMLFooter();
?>
  • 写回答

1条回答 默认 最新

  • doulu4203 2016-05-29 18:19
    关注

    Replace header functions parameter - "static function checkAuthWithRedirect()" in auth.php with the following

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

    It is because, when you place redirect to test/admin.php in doLogin() within auth.php,

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

    Just replace the redirect value in checkAuthWithRedirect() to test/admin.php & redirect value in doLogin() to test/admin.php. It would work!

    评论

报告相同问题?

悬赏问题

  • ¥17 pro*C预编译“闪回查询”报错SCN不能识别
  • ¥15 微信会员卡接入微信支付商户号收款
  • ¥15 如何获取烟草零售终端数据
  • ¥15 数学建模招标中位数问题
  • ¥15 phython路径名过长报错 不知道什么问题
  • ¥15 深度学习中模型转换该怎么实现
  • ¥15 HLs设计手写数字识别程序编译通不过
  • ¥15 Stata外部命令安装问题求帮助!
  • ¥15 从键盘随机输入A-H中的一串字符串,用七段数码管方法进行绘制。提交代码及运行截图。
  • ¥15 TYPCE母转母,插入认方向