2012-02-25 68 views
0

我有一個管理員頁面(admin.php),我現在正在設置。無法弄清楚我的PHP代碼有什麼問題

當頁面最初訪問,登錄框出現正確,用戶可以打一個「登錄」按鈕。

當他們點擊「登錄」按鈕,登錄表單被提交到一個PHP頁面代碼(我沒有任何身份驗證的事情現在 - 剛開始得到這個設置):

<?php 
    session_start(); 
    $_SESSION['login_success'] = true; 

    header('Location:http://localhost/mbc/admin'); 
    exit(); 

?> 

然後,我期待的是,admin.php的頁面將顯示管理形式,但頁面只是顯示了重定向後的空白。以下是admin.php頁面的適用部分。你們中的任何一個都可以看到我在這裏做錯了什麼,以至於在認證完成後管理員表單不顯示?

<html> 
    <head> 
     <title>Welcome Home!</title> 
     <link href="style.css" rel="stylesheet" type="text/css" /> 
     <link href="layout.css" rel="stylesheet" type="text/css" /> 
     <script type="text/javascript" src="jquery.min.js"></script> 
     <style> 
      /* Mask for background, by default is not display */ 
      #mask { 
       display: none; 
       background: #000; 
       position: fixed; left: 0; top: 0; 
       z-index: 10; 
       width: 100%; height: 100%; 
       opacity: 0.8; 
       z-index: 999; 
      } 

      /* You can customize to your needs */ 
      .login-popup{ 
       display:none; 
       background: #333; 
       padding: 10px; 
       border: 2px solid #ddd; 
       float: left; 
       font-size: 1.2em; 
       position: fixed; 
       top: 50%; left: 50%; 
       z-index: 99999; 
       box-shadow: 0px 0px 20px #999; /* CSS3 */ 
       -moz-box-shadow: 0px 0px 20px #999; /* Firefox */ 
       -webkit-box-shadow: 0px 0px 20px #999; /* Safari, Chrome */ 
       border-radius:3px 3px 3px 3px; 
       -moz-border-radius: 3px; /* Firefox */ 
       -webkit-border-radius: 3px; /* Safari, Chrome */ 
      } 

      img.btn_close { Position the close button 
       float: right; 
       margin: -28px -28px 0 0; 
      } 

      fieldset { 
       border:none; 
      } 

      form.signin .textbox label { 
       display:block; 
       padding-bottom:7px; 
      } 

      form.signin .textbox span { 
       display:block; 
      } 

      form.signin p, form.signin span { 
       color:#999; 
       font-size:11px; 
       line-height:18px; 
      } 

      form.signin .textbox input { 
       background:#666666; 
       border-bottom:1px solid #333; 
       border-left:1px solid #000; 
       border-right:1px solid #333; 
       border-top:1px solid #000; 
       color:#fff; 
       border-radius: 3px 3px 3px 3px; 
       -moz-border-radius: 3px; 
       -webkit-border-radius: 3px; 
       font:13px Arial, Helvetica, sans-serif; 
       padding:6px 6px 4px; 
       width:200px; 
      } 

      form.signin input:-moz-placeholder { color:#bbb; text-shadow:0 0 2px #000; } 
      form.signin input::-webkit-input-placeholder { color:#bbb; text-shadow:0 0 2px #000; } 

      .button { 
       background: -moz-linear-gradient(center top, #f3f3f3, #dddddd); 
       background: -webkit-gradient(linear, left top, left bottom, from(#f3f3f3), to(#dddddd)); 
       background: -o-linear-gradient(top, #f3f3f3, #dddddd); 
       filter: progid:DXImageTransform.Microsoft.gradient(startColorStr='#f3f3f3', EndColorStr='#dddddd'); 
       border-color:#000; 
       border-width:1px; 
       border-radius:4px 4px 4px 4px; 
       -moz-border-radius: 4px; 
       -webkit-border-radius: 4px; 
       color:#333; 
       cursor:pointer; 
       display:inline-block; 
       padding:6px 6px 4px; 
       margin-top:10px; 
       font:12px; 
       width:214px; 
      } 
      .button:hover { background:#ddd; } 
     </style> 
    </head> 

    <body id="page1"> 
     <?php 
      session_start(); 
      if (isset($_SESSION['login_success'])) { 
     ?> 
     <!-- Some HTML content should show up here but it isn't... --> 

     <?php } else { ?> 

     <!-- Login Dialog --> 
     <div id="login-box" class="login-popup"> 
      <a href="index.html">Cancel</a> 
      <form method="post" class="signin" action="admin_process_login.php"> 
       <fieldset class="textbox"> 
        <label class="username"> 
         <span>Username</span> 
         <input id="username" name="username" value="" type="text" autocomplete="on" placeholder="Username"> 
        </label> 
        <label class="password"> 
         <span>Password</span> 
         <input id="password" name="password" value="" type="password" placeholder="Password"> 
        </label> 
        <button class="login" type="submit">Sign in</button>  
       </fieldset> 
      </form> 
     </div> 

     <script type="text/javascript"> 
      $(document).ready(function() { 

        //Getting the variable's value from a link 
        var loginBox = document.getElementById('login-box'); 

        //Fade in the Popup 
        $(loginBox).fadeIn(300); 

        //Set the center alignment padding + border see css style 
        var popMargTop = ($(loginBox).height() + 24)/2; 
        var popMargLeft = ($(loginBox).width() + 24)/2; 

        $(loginBox).css({ 
         'margin-top' : -popMargTop, 
         'margin-left' : -popMargLeft 
         }); 

        // Add the mask to body 
        $('body').append('<div id="mask"></div>'); 
        $('#mask').fadeIn(300); 

        // When clicking on the button close or the mask layer the popup closed 
        $('button.login').live('click', function() { 
          $('#mask , .login-popup').fadeOut(300 , function() { 
           $('#mask').remove(); 
           }); 
          return false; 
          }); 
      }); 
     </script> 

     <?php } ?> 

    </body> 
</html> 

編輯2012-02-25 16:54EST
一些奇怪的原因,這種情況(僅此就我到目前爲止已測試)的一系列事件使得管理表單拿出正確...
*去admin.php的,並點擊「登錄」按鈕
*去「測試」 PHP頁面瀏覽器(HTTP://本地主機/ MBC /測試)
代碼測試頁:

<html> 
    <head> 
     <title>Testing</title> 
    </head> 
    <body> 
    <?php 
     session_start(); 
     if (isset($_SESSION['login_success'])) { ?> 
     <H1>Login was a success</H1> 
     <?php } else { ?> 
     <H1>Login was a failure - next time it should work</H1> 
     <?php 
     $_SESSION['login_success'] = true; 
     } 
     ?> 
    </body> 
</html> 
  • 進入管理頁面(HTTP://本地主機/ MBC/admin)和現在的管理形式出現的罰款。
+0

白頁意味着您需要現在檢查錯誤日誌。 – hakre 2012-02-25 21:34:11

+0

不應該是'header('Location:http:// localhost/mbc/admin。php');'或者你的文件實際上缺少擴展名。如果它是一個文件夾,重定向應以斜槓結尾,然後通常會加載索引文件(如果存在)。 – adeneo 2012-02-25 21:34:28

+0

@hakre - 上面忘記提到當我在白頁上查看源代碼時,「<?php} else {?>」下面的內容會回來 - 所以就好像會話沒有正確設置一樣。 .. – 2012-02-25 21:39:25

回答

0

這個問題實際上是jQuery的點擊按鈕的代碼,特別是「return false」部分:

// When clicking on the button close or the mask layer the popup closed 
$('button.login').live('click', function() { 
    $('#mask , .login-popup').fadeOut(300 , function() { 
     $('#mask').remove(); 
    }); 
    return false; 
}); 

當我刪除該代碼,「$_SESSION['login_success'] = true;」發生得很好,並且管理員表單部分成功返回。

原來「return false」有一些討厭的副作用,應謹慎使用。有關使用「return false」的更多信息,請參閱 http://fuelyourcoding.com/jquery-events-stop-misusing-return-false/

-1

我用不上,現在來測試它,但我覺得你的問題是,它應該是:

header('Location: http://localhost/mbc/admin'); 

參考:http://php.net/manual/en/function.header.php

無論是引用表明,在URI之前需要「:」之後的空格。

-1

你的代碼沒有問題。你只是期望start_session()不工作,而$ _SESSION ['login_success']是未設置的。我建議你使用一個php框架或閱讀關於會話的指南。

這裏,改變這樣和形式彈出:

<?php 
       //session_start(); // if you uncomment print_r will give you 1 
       if (isset($_SESSION['login_success'])) { 
        echo '<pre>'; 
        print_r($_SESSION['login_success']); 
      ?> 
      <!-- Some HTML content should show up here but it isn't... --> 
        empty statement</pre> 
+0

不是 - 沒有工作。請參閱我上面的編輯。 – 2012-02-25 22:18:40

+0

夥計。閱讀指南。 http://www.php-learn-it.com/php_sessions.html:注意:「session_start()函數必須在標記」 – 2012-02-26 02:25:33

+0

「之前出現我已經閱讀了許多網站,並且已將session_start()放在頂部所有我的腳本。沒有必要對我的問題進行投票表決,但顯然需要對您的答案進行降級投票,因爲您現在給我提供了兩次信息,但沒有讓我找到解決方案。除非你完全確定它會起作用,否則請不要發佈任何其他內容。 – 2012-02-26 11:36:31

相關問題