English 中文(简体)
无法让php 设置会话
原标题:Can t get php to set sessions

我只是不能把会话设置在php, 我无法找到原因。

希望你能给我一个线索 说明我可能在哪里做错事!

指数.php

<?php
    // Inialize session
    session_start();

    include_once("commons/config.php");
    $authenticated = checkLoggedIn("yes", FALSE);
    //flushMemberSession();

    var_dump($authenticated);

    echo "<pre>";
    var_dump($_SESSION);
    echo "</pre>";
?>
... followed by html and some php ifs

action.php 动作。php checkPass () 中, action.php 动作。php 中,该呼叫应来自 checkPass ()

$( #login ).click(function(){
    var data = $( #login-form ).serialize();
    $.post( commons/action.php 动作。php , data, function(result){
        if(result == true){
            console.log(result);
            //location.reload();
        }else{
            console.log( not authenthicated );
        }
    }, json );
    return false;
});

action.php 动作。php

session_start();
if (!$_SERVER[ SERVER_ADDR ] == $_SERVER[ REMOTE_ADDR ]){
    header( HTTP/1.0 400 Unauthorized , true, 400);
    exit;
} else {

    include( config.php );

    $user = mysql_real_escape_string(stripslashes($_POST[ username ]));
    $pass = mysql_real_escape_string(stripslashes($_POST[ password ]));

    $response = checkPass($user, $pass); // false or

    $debug = array( user =>$user,  pass =>$pass,  response =>$response);

    print_r(json_encode($response));

}

函数 :

function checkLoggedIn($status, $redirect=TRUE){
    switch($status){
        case "yes":
            if(!isset($_SESSION["loggedIn"])){
                if($redirect) {
                    header("Location: login.php");
                    exit;
                } else {
                    $authenticated = false;
                    return $authenticated;
                }
            } else {
                checkLoggedIn("no");
            }
        break;
        case "no":
            if(isset($_SESSION["loggedIn"]) && $_SESSION["loggedIn"] === true ){
                //header("Location: members.php");
                $authenticated = true;
                return $authenticated;
            }
        break;
    }   
    return true;
}

function checkPass($username, $password) {
    $query="SELECT username, password FROM users WHERE username= $username  and password= $password ";
    $result=mysql_query($query, $link) or die("checkPass fatal error: ".mysql_error());

// Check exactly one row is found:
if(mysql_num_rows($result)==1) {
    cleanMemberSession($username);
    return true;
    /*$row=mysql_fetch_array($result);
    return $row;*/
}
//Bad username:
return false;
}

function cleanMemberSession($username) {
session_regenerate_id();
$_SESSION["username"]=$username;
$_SESSION["loggedIn"]=true;
session_write_close();
}

UPDATE

AJAX 信头

Response Headers
Connection  Keep-Alive
Content-Length  4
Content-Type    text/html
Date    Sun, 27 May 2012 19:36:54 GMT
Keep-Alive  timeout=5, max=100
Server  Apache/2.2.21 (Win32) mod_ssl/2.2.21 OpenSSL/1.0.0e PHP/5.3.8 mod_perl/2.0.4 Perl/v5.10.1
X-Powered-By    PHP/5.3.8
Request Headers
Accept  application/json, text/javascript, */*; q=0.01
Accept-Encoding gzip, deflate
Accept-Language en-us,en;q=0.5
Connection  keep-alive
Content-Length  31
Content-Type    application/x-www-form-urlencoded; charset=UTF-8
Cookie  PHPSESSID=qhbjq76f4np7iug09jrnl4j5j1
Host    localhost
Referer http://localhost/tw/Tevienes/web/
User-Agent  Mozilla/5.0 (Windows NT 6.1; WOW64; rv:12.0) Gecko/20100101 Firefox/12.0
X-Requested-With    XMLHttpRequest

UPDATE2

BTW, 对 var_dump ($_SESSION); array(0) {{_/code> 的响应

UPDATE3

I just added $_SESSION[ test ] = alex ; after session_start(); in 指数.php, and the session variable has been set... so It must be something with the function setting the variables... or who know what else

最佳回答

Put session_start() in all called .php files, also the ones called from ajax. Also at the start of actions.php put parentheses on the comparisons. ! operator has higher precedence than < or >.

变动

 if (!$_SERVER[ SERVER_ADDR ] == $_SERVER[ REMOTE_ADDR ]){

 if (!($_SERVER[ SERVER_ADDR ] == $_SERVER[ REMOTE_ADDR ])){
问题回答

长镜头和短镜头, 但检查在服务器和客户端上的日期和时间都设置正确。 如果其中任何一个设置错误, 会话 cookie 将/ 可能无法设置 。





相关问题
SQL SubQuery getting particular column

I noticed that there were some threads with similar questions, and I did look through them but did not really get a convincing answer. Here s my question: The subquery below returns a Table with 3 ...

please can anyone check this while loop and if condition

<?php $con=mysql_connect("localhost","mts","mts"); if(!con) { die( unable to connect . mysql_error()); } mysql_select_db("mts",$con); /* date_default_timezone_set ("Asia/Calcutta"); $date = ...

php return a specific row from query

Is it possible in php to return a specific row of data from a mysql query? None of the fetch statements that I ve found return a 2 dimensional array to access specific rows. I want to be able to ...

Character Encodings in PHP and MySQL

Our website was developed with a meta tag set to... <meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1" /> This works fine for M-dashes and special quotes, etc. However, I ...

Pagination Strategies for Complex (slow) Datasets

What are some of the strategies being used for pagination of data sets that involve complex queries? count(*) takes ~1.5 sec so we don t want to hit the DB for every page view. Currently there are ~...

Averaging a total in mySQL

My table looks like person_id | car_id | miles ------------------------------ 1 | 1 | 100 1 | 2 | 200 2 | 3 | 1000 2 | 4 | 500 I need to ...