php,android:会话变量不会设置

问题描述

在Android Studio中处理登录脚本。它将api调用发送到在WAMP中运行的PHP脚本。一切正常,除非会话变量设置不正确。疯狂的事情是,它完全适合邮递员使用。它以应有的方式返回用户标识。当我从仿真器运行登录名时,它工作正常并且没有显示错误。问题是我无法将userid设置为会话变量,以便可以在其他PHP页面(程序的其余部分)中使用它。我设置了一个测试会话变量PHP文件,它将会话变量设置得很好。我可以从其他PHP文件访问它们。我什至在脚本的顶部放置了一个测试脚本-$ _SESSION ['mystery'] =“问题”; -设置得很好,我可以从其他PHP文件访问它。我从来没有这么难过。它可能与WAMP不允许从和android调用设置会话变量有关吗?我没有尝试过将它放在实时服务器上。我不知道它的行为是否会有所不同。有什么想法吗?

这是我的登录API:

<?PHP
session_start();

$response = array();
// Check if the user is already logged in,if yes then redirect him to welcome page

if(isset($_SESSION["loggedin"]) && $_SESSION["loggedin"] === true){
    echo "already logged in";
    //header("location: welcome.PHP");
    //exit;
}

// Include config file
require_once "configuration.PHP";

 $_SESSION['mystery'] = "problem";


// Define variables and initialize with empty values
$username = "";
$password = "";

$username_err = "";
$password_err = "";

// Processing form data when form is submitted
if($_SERVER["REQUEST_METHOD"] == "POST"){

// Check if username is empty
if(empty(trim($_POST["username"]))){
    $username_err = "Please enter username.";
} else{
    $username = trim($_POST["username"]);
}

// Check if password is empty
if(empty(trim($_POST["password"]))){
    $password_err = "Please enter your password.";
} else{
    $password = trim($_POST["password"]);
}

// Validate credentials
if(empty($username_err) && empty($password_err)){
    // Prepare a select statement
    $sql = "SELECT id,username,password FROM users WHERE username = ?";
    
    if($stmt = MysqLi_prepare($conn,$sql)){
        // Bind variables to the prepared statement as parameters
        MysqLi_stmt_bind_param($stmt,"s",$param_username);
        
        // Set parameters
        $param_username = $username;
        
        // Attempt to execute the prepared statement
        if(MysqLi_stmt_execute($stmt)){
            // Store result
            MysqLi_stmt_store_result($stmt);
            
            // Check if username exists,if yes then verify password
            if(MysqLi_stmt_num_rows($stmt) == 1){                    
                // Bind result variables
                MysqLi_stmt_bind_result($stmt,$id,$username,$hashed_password);
                if(MysqLi_stmt_fetch($stmt)){
                    if(password_verify($password,$hashed_password)){
                        // Password is correct,so start a new session
                        session_start();
                        
                        $user = array(
                        'id'=>$id,'username'=>$username
                        );
                        
                        $response['error'] = false; 
                        $response['message'] = 'Login successfull'; 
                        $response['user'] = $user; 
                        
                        // Store data in session variables
                       // $_SESSION["loggedin"] = true;
                        
                         $_SESSION['id'] = $user['id'];
                        // Redirect user to welcome page
                        //header("location: welcome.PHP");
                    } else{
                        // display an error message if password is not valid
                        $password_err = "The password you entered was not valid.";
                    }
                }
            } else{
                // display an error message if username doesn't exist
                $username_err = "No account found with that username.";
            }
        } else{
            echo "Oops! Something went wrong. Please try again later.";
        }

        // Close statement
        MysqLi_stmt_close($stmt);
    }
}

// Close connection
MysqLi_close($conn);

echo json_encode($response).'<br><br>';
print_r ($_SESSION).'<br><br>';


}
?>

解决方法

暂无找到可以解决该程序问题的有效方法,小编努力寻找整理中!

如果你已经找到好的解决方法,欢迎将解决方案带上本链接一起发送给小编。

小编邮箱:dio#foxmail.com (将#修改为@)