2016-05-29 11 views
0

このコードを使用する私は少し問題に答えました。サブフォルダーにリダイレクトしようとすると、登録ユーザーは無視され、index.phpサイトにリダイレクトされます。ルートフォルダにあるが、私はそれをサブフォルダにリダイレクトしようとすると、completilyコードを無視して、index.phpに私をリダイレクトするサイトに私をリダイレクトするときPHPとMySQLのログ記録/リダイレクトに関する問題

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

コードのこのブロックは正常に動作します。

例:

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

あなたはここで404を取得していないと確信していますか? 'test'がスクリプトの実行場所のサブフォルダでない場合は、ルート' header( "Location:/test/admin.php")に戻ります。 exit;または完全なhttp呼び出し 'header("場所:http://www.example.com/test/admin.php "); exit; ' - エラーもチェックします。 –

+0

あなたは '.htaccess'とタグ付けされていますので、書き直しが悪いかどうかわかりません。もう一度、エラーをチェックしたり、ログに何かがあるかどうかを確認してください。私は1つは魔法の答えでポップすることができないためです。がんばろう。私はこれを渡して移動します。 –

+0

.htaccessが私のタグに失敗しました。心配しないで、自分で答えを探している間に質問を投稿しました。ありがとうございました。私は間違いを探すでしょう – Svinjica

答えて

0

ヘッダ関数パラメータを置換する - 以下とauth.phpにおける "静的関数checkAuthWithRedirect()"

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

これは、あなたはdoLogin()auth.php内で/ admin.php、

MYAUTH :: checkAuthWithRedirectを()をテストするためにリダイレクト配置すると、テストするadmin.phpだけcheckAuthWithRedirect(におけるリダイレクト値を置き換えることが再びのindex.php

にリダイレクトさcheckAuthWithRedirect()でauth.phpを開始)/admin.php & のリダイレクト値doLogin()を/ admin.phpにテストします。それは動作します!

+0

私はこれをやろうとしましたが、うまくいかず、以下のURLが作成されます:** test/test/admin.php **そして私はこのURLにリダイレクトする必要があります** test/admin.php **。公正な機能を実現するには、checkAuth()_を使用して現場で検証を行います。 _ユーザーがログインしていない場合はindex.php_にリダイレクトしますが、私はあなたの努力に感謝します、ありがとうございました! – Svinjica

関連する問題