登录后在$ _SESSION中存储$ username和$ file变量

时间:2017-05-16 10:51:20

标签: php html session

index.php 我用$ _POST

获取用户名和密码fileds的值

的index.php

if(isset($_POST["username"]) && isset($_POST["password"])){

    $username = mysql_real_escape_string(strtolower($_POST['username']));
    $password = mysql_real_escape_string($_POST['password']);

    $_SESSION['username'] = $username;
    $_SESSION['password'] = $password;

    checkUser($_SESSION['username'], $_SESSION['password']);
}

然后我将这些$ username和$ password变量存储在$ _SESSION中并调用函数checkUser($ _ SESSION [' username'],$ _SESSION [' password'])) ;它发送两个参数。 checkUser()函数在 lib.php

中执行

lib.php

session_start();

function checkUser($username, $password){
    include "connection.php";
    $result = mysqli_query($conn, "SELECT * FROM `data` WHERE `username` = '$username' AND `password` = '$password'") or die("No result".mysqli_error());

    $row = mysqli_fetch_array($result);
    $logic = false;

    if (($row['username'] == $username) && ($row['password'] == $password)) {

        $logic = true;

        echo "HI,".$username; 
?>

  <a href='logout.php'>Log Out</a>

<?php

            $file = $row['file'];
            echo "<img src='images/users/".$file."' >";
}
    else{
        echo "Failed to login. Username or password is incorrect. Try again.";
    }
}

此部分用于显示用户的名称和根据它的图像。

  

logout.php正常工作

logout.php

unset($_SESSION["username"]);
unset($_SESSION["password"]);
unset($_SESSION["file"]);
header("Location: index.php");

session_destroy();

问题是当我从一个页面导航到另一个页面时,$ _SESSION变量变为空。会话出了点问题。请帮帮我。

1 个答案:

答案 0 :(得分:1)

在php页面中,您需要在启动<?php代码

之后访问会话变量gradlew android:assembleDebug --profile