2015-09-05 60 views
2

我有一個登錄頁面,我想設置會話。但是我的會話沒有設置!可以在php中設置會話設置

<!-- 
Author: Dibyendu Konar 

--> 
<?php 
session_start(); 
print_r($_SESSION); 
include "db.php"; 
?> 
<!DOCTYPE html> 
<html> 
<head> 
    <title>Handikap admin login</title> 
     <meta charset="utf-8"> 
     <link href="css/style.css" rel='stylesheet' type='text/css' /> 
     <meta name="viewport" content="width=device-width, initial-scale=1"> 
     <script type="application/x-javascript"> addEventListener("load", function() { setTimeout(hideURLbar, 0); }, false); function hideURLbar(){ window.scrollTo(0,1); } </script> 
     <!--webfonts--> 
     <link href='http://fonts.googleapis.com/css?family=Open+Sans:600italic,400,300,600,700' rel='stylesheet' type='text/css'> 
     <!--//webfonts--> 
</head> 
<body> 
    <!-----start-main----> 
    <div class="main"> 
     <div class="login-form"> 
      <h1>Admin Login</h1> 
        <div class="head"> 
         <img src="images/user.png" alt=""/> 
        </div> 
       <form method="POST" onsubmit="errorCheck()"> 
         <input type="text" name="email" id="email" class="text" value="EMAIL" onfocus="this.value = '';" onblur="if (this.value == '') {this.value = 'EMAIL';}" > 
         <input type="password" id="password" name="password" value="PASSWORD" onfocus="this.value = '';" onblur="if (this.value == '') {this.value = 'Password';}"> 
         <div class="submit"> 
          <input type="submit" name="submit" value="LOGIN" > 
        </div> 
        <p><a href="<?php echo "./login"; ?>">Forgot Password ?</a></p> 
       </form> 
      </div> 
      <!--//End-login-form--> 

     </div> 
      <!-----//end-main----> 

</body> 
    <script src="https://code.jquery.com/jquery-1.10.2.js"></script> 
<script type="text/javascript" charset="utf-8" async defer> 
function errorCheck() 
{ 
    var password = $('#password').val(); 
    var email = $('#email').val(); 
    if(((password == '') || (password == 'password'))|| ((email == '') || (email == 'EMAIL'))) 
    { 
     alert("Please enter values"); 
     return false; 
    } 
} 

</script> 
</html> 


<?php 
function curPageURL() { 
$pageURL = 'http'; 
if ($_SERVER["HTTPS"] == "on") {$pageURL .= "s";} 
$pageURL .= "://"; 
if ($_SERVER["SERVER_PORT"] != "80") { 
    $pageURL .= $_SERVER["SERVER_NAME"].":".$_SERVER["SERVER_PORT"].$_SERVER["REQUEST_URI"]; 
} else { 
    $pageURL .= $_SERVER["SERVER_NAME"].$_SERVER["REQUEST_URI"]; 
} 
return $pageURL; 
} 
function redirect($url) { 
    if(!headers_sent()) { 
     //If headers not sent yet... then do php redirect 
     header('Location: '.$url); 
     exit; 
    } else { 
     //If headers are sent... do javascript redirect... if javascript disabled, do html redirect. 
     echo '<script type="text/javascript">'; 
     echo 'window.location.href="'.$url.'";'; 
     echo '</script>'; 
     echo '<noscript>'; 
     echo '<meta http-equiv="refresh" content="0;url='.$url.'" />'; 
     echo '</noscript>'; 
     exit; 
    } 
} 


if(isset($_POST['submit'])) 
{ 
    /*-------------Getting the value------------*/ 
    $emailId  = trim($_POST["email"]); 
    $passwordAdmin = trim($_POST["password"]); 
    /*------------------------------------------*/ 

    //echo $emailId.'<br/>'.$passwordAdmin; 
    $result =mysql_query("SELECT * FROM `admin`"); 
    if(mysql_num_rows($result)>0) 
       { 
        while($row=mysql_fetch_assoc($result)) 
         { 
          extract($row); 
          echo '<pre/>'; 
          $values[] = $row; 
         } 
       } 
    $databaseAdminEmail = $values[0]['email']; 
    $databaseAdminPassword = $values[0]['password']; 
    //echo $databaseAdminEmail.'<br/>'.$databaseAdminPassword; 
    /*-----------------Validation debug--------------------------*/ 

    if ($databaseAdminEmail != $emailId) 
     { 
      header('Location: '.$_SERVER['PHP_SELF'].'?auth=user_not_found'); 

     } 

    if($databaseAdminPassword != $passwordAdmin) 
     { 
      header('Location: '.$_SERVER['PHP_SELF'].'?auth=password_error'); 
      //echo "stupid"; 
     } 
    else 
     { 

       $_SESSION["admin"] = "log_in"; 
       echo "testpassed"; 
       echo $_SESSION['login']; 
       $newURLExploded = explode("/admin.php", $newURL); 
       $targetUrl = "playerlist.php"; 
       redirect($targetUrl); 

       //die(); 
     }  
} 
?> 

,因此不開始能夠同時接取入anotherfolder在同一個項目

其中編碼狹窄是這樣

<!DOCTYPE html> 
<?php 
session_start(); 
echo $_SESSION['admin']; 
include "db.php"; 
?> 
<html> 

<head> 
    <meta charset='UTF-8'> 

    <title>User information</title> 

    <meta name="viewport" content="width=device-width, initial-scale=1.0"> 

    <link rel="stylesheet" href="css/style.css"> 

    <!--[if !IE]><!--> 
    <style> 

    /* 
    Max width before this PARTICULAR table gets nasty 
    This query will take effect for any screen smaller than 760px 
    and also iPads specifically. 
    */ 
    @media 
    only screen and (max-width: 760px), 
    (min-device-width: 768px) and (max-device-width: 1024px) { 

     /* Force table to not be like tables anymore */ 
     table, thead, tbody, th, td, tr { 
      display: block; 
     } 

     /* Hide table headers (but not display: none;, for accessibility) */ 
     thead tr { 
      position: absolute; 
      top: -9999px; 
      left: -9999px; 
     } 

     tr { border: 1px solid #ccc; } 

     td { 
      /* Behave like a "row" */ 
      border: none; 
      border-bottom: 1px solid #eee; 
      position: relative; 
      padding-left: 50%; 
     } 

     td:before { 
      /* Now like a table header */ 
      position: absolute; 
      /* Top/left values mimic padding */ 
      top: 6px; 
      left: 6px; 
      width: 45%; 
      padding-right: 10px; 
      white-space: nowrap; 
     } 

     /* 
     Label the data 
     */ 

    } 

    /* Smartphones (portrait and landscape) ----------- */ 
    @media only screen 
    and (min-device-width : 320px) 
    and (max-device-width : 480px) { 
     body { 
      padding: 0; 
      margin: 0; 
      width: 320px; } 
     } 

    /* iPads (portrait and landscape) ----------- */ 
    @media only screen and (min-device-width: 768px) and (max-device-width: 1024px) { 
     body { 
      width: 495px; 
     } 
    } 

    </style> 
    <!--<![endif]--> 

</head> 

<body> 

    <div id="page-wrap"> 



    <table> 
     <thead> 
     <tr> 
      <th>Player id</th> 
      <th>Percentage of complition</th> 
      <th>Tag</th> 
      <th>Device</th> 

     </tr> 
     </thead> 
     <tbody> 



<?php 


     $result =mysql_query("SELECT * FROM `handikap`"); 
      if(mysql_num_rows($result)>0) 
       { 
        while($row=mysql_fetch_assoc($result)) 
         { 
          extract($row); 
          echo '<pre/>'; 
          $values[] = $row; 
         } 
       } 
     $sizeOfArray = sizeof($values); 
     if($sizeOfArray >= 0) 
       { 
         for($i=0;$i<$sizeOfArray;$i++) 
         { 

          echo "<tr> 
      <td>".$values[$i]['playerId']."</td> 
      <td>".$values[$i]['percentageComplete']."%"."</td> 
      <td>".$values[$i]['tag']."</td> 
      <td>".$values[$i]['device']."</td> 

     </tr>"; 
         } 
       } 
?> 
     </tbody> 
    </table> 

    </div> 

</body> 

</html> 

的現在的問題是,我想提出的錯誤... ???? 因爲我已經設置了會話以及獲取它也因此沒有出來.... !!!!!

,我想摧毀我的會話logout.php

<?php 
session_destroy(); 
unset($_SESSION["admin"]); 
$target = "admin.php"; 
header('Location: ' . $target) ; 
?> 

因此會議不破壞

+0

請勿將會話設置在您的html頂部 – aldrin27

+0

在您的代碼中,似乎您在$ _SESSION ['login']'中沒有任何內容,但'$ _SESSION ['admin']' 。 – Jimmy

+0

hi @Jimmy我在管理員也設置了它,因此沒有輸出 –

回答

2

PHP documentation:

要使用基於cookie的會話,在session_start()必須在向瀏覽器輸出任何內容之前被調用。

+0

也就是說,你必須在任何HTML之上都有'session_start()'。 – Wille

+0

我已經完成了 –

0

如果你的php文件時不會相互鏈接(如MVC結構,例如),你必須在每個文件中聲明session_start()

0

你是不是在$ _SESSION [「登錄」進行任何設置]。你只需要$ _SESSION ['admin'] ='log_in'。 如上所述,無論您想使用會話(設置或獲取會話中的值),您都需要在訪問$ _SESSION []之前使用session_start()函數。