-1

我的 jquery 移动网站有问题。它的会话不工作。在服务器 register_global 在 php.ini 中关闭

有人请建议我如何在每个页面上获取会话数据。我在每个页面上都使用了 session_start。以下是我的php代码。

<?php

include("include/connect.php");
if(isset($_POST['submitted'])){ 

// $_POST['username'] and $_POST['password'] are the param names we sent in our click event
$email = $_POST['email'];  
$password = md5($_POST['password']);
// Select eveything from the users table where username field == the username we posted and password field == the password we posted  
$sql = "SELECT * FROM user WHERE email = '" . $email . "' AND password = '" . $password . "'";  
$query = mysql_query($sql);

// If we find a match, create an array of data 
if (mysql_num_rows($query) > 0)  
{  
    $row = mysql_fetch_array($query);  
    $response = array(  
        'logged' => true,
        'id'    =>$row['id'],           
        'name' => $row['fullname'],  
        'email' => $row['email']  
    );
    session_start();
    $_SESSION['name']=$response['name'];
    $_SESSION['email']=$response['email'];
    $_SESSION['id']=$response['id'];
    //redirect to homepage
    header('Location: index.php');
}  
else  
{  
    // Else the username and/or password was invalid! Create an array, json_encode it and echo it out  
    $response = array(  
        'logged' => false,  
        'message' => 'Invalid Username and/or Password'  
    );  
    //echo json_encode($response);  
    $msg="<font color='red'> invalid username or password, try again</font>";
    echo $msg;
} 
}
4

1 回答 1

0

更多评论,但它可能会解决您的问题。会话必须在发送任何输出之前启动。通常的做法是将这段代码作为文件顶部的第一件事包括在内,如下所示:

 <?php
 if(!isset($_SESSION)){  // don't start it if it already is
      session_start();
 }

 // ... now do stuff
于 2013-07-24T18:23:32.383 回答