簡體   English   中英

無法弄清楚我的PHP代碼出了什么問題

[英]Can't figure out what's wrong with my PHP code

我現在正在設置一個管理頁面(admin.php)。

最初訪問該頁面時,將正確顯示一個登錄框,用戶可以在其中單擊“登錄”按鈕。

當他們點擊“登錄”按鈕時,登錄表單將使用以下代碼提交到PHP頁面(我現在沒有進行任何身份驗證-只是開始進行此設置):

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

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

?>

然后,我期望admin.php頁面將顯示admin表單,但重定向后頁面僅顯示空白。 以下是admin.php頁面的適用部分。 你們中的任何人都能看到我在這里做錯了什么,以便在身份驗證完成后不顯示admin表單嗎?

<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:// localhost / mbc / test)
測試頁代碼:

<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:// localhost / mbc / admin),現在可以正常使用管理表單。

問題實際上出在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/

我目前無權對其進行測試,但是我認為您的問題是:

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

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

這兩個引用均表明URI之前需要在“:”之后加一個空格。

您的代碼沒有錯。 您只是希望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>

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM