可以在php中设置set session

时间:2015-09-05 10:09:26

标签: php session

我有一个登录页面,我想设置会话。但是我的会话没有设置!!!!

<!--
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();
        }       
}
?>

因此无法开始将其同时转移到同一项目的另一个文件夹中

编码限制就像这样

<!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 ) ;
?>

因此会议没有破坏

3 个答案:

答案 0 :(得分:2)

来自PHP documentation:

  

要使用基于cookie的会话,必须在向浏览器输出任何内容之前调用session_start()。

答案 1 :(得分:0)

如果您的php文件没有相互链接(例如MVC结构),则必须在每个文件中声明session_start()

答案 2 :(得分:0)

您没有在$ _SESSION ['login']中设置任何内容。你只需要$ _SESSION ['admin'] ='log_in'。 如此处所述,无论您想要使用会话(从会话中设置或获取值),您都需要在访问$ _SESSION []之前使用session_start()函数。