在会话中存储数据

时间:2017-11-06 10:50:35

标签: php html web

我有一个关于从数据库中检索信息的问题。 当我第一次登录时,数据会显示在下一页(processlogin.php)上,其中包含性别和名称等内容。但是,当我单击继续时,主页上不会显示数据。但是,当我注销并使用相同的用户名和密码登录时,信息将出现在主页上。

<!DOCTYPE html>
<html>

<body>

    <div id="nav">

    <form  action="intropage.php" method="post"> 
    </form>

    <?php
    header('Access-Control-Allow-Origin: *');
    header('Access-Control-Allow-Methods: GET, POST, PATCH, PUT, DELETE, OPTIONS');
    header('Access-Control-Allow-Headers: Origin, Content-Type, X-Auth-Token');
    header("Content-Type: application/json; charset=UTF-8");

    error_reporting(E_ERROR); 

    include("global.php");



    if (!empty($_POST['userid']))
    {
        $userid=$_POST["userid"];
    }
    else 
    {
        $userid = null; 
        echo '<p><font color="red">Please enter a Userid!</font></p>';
    }

    //$_SESSION['userid'] = $userid;

    if (!empty($_POST['password']))
    {
        $password=$_POST["password"];

    }
    else 
    {
        $password = null;
        echo '<p><font color="red">Please enter a Password!</font></p>';
    }

        if ($userid && $password) 
        {
        session_start();
        $_SESSION['userid'] = $_POST['userid'];


        if (isset ($_SESSION['userid']) == false)
        {
            header ("Location: login.php");
        }

        $mysqli = new mysqli(Petshop, dbuser, dbpw, db);

        $stmt = $mysqli->prepare("SELECT password,name,gender FROM users WHERE userid=?");
        $stmt->bind_param("s", $userid);
        $stmt->execute();
        $stmt->bind_result($p, $name, $gender); 
        $stmt->fetch();

            if ($p == $password) {
            header ("Location: intropage.php");
            echo "$userid";
            echo "$password";
            echo "$name";
            echo "$gender";
            echo 'Click Here to <a href="intropage.php">Proceed</a><br>';

            }
            else {
            echo '<p><h1>Login is not successful, please try again</h1>';
            echo 'Click here to <a href="login.php">login</a><br>';
            }
        }
            $stmt->close();
            $mysqli->close();


    ?>

    </div>

    <div id="footer-push">
    </div>

    <div id="footer">
        Copyright &copy; Happy Pet Shop
    </div>

</body>
</html>


    <?php
    header('Access-Control-Allow-Origin: *');
    header('Access-Control-Allow-Methods: GET, POST, PATCH, PUT, DELETE, OPTIONS');
    header('Access-Control-Allow-Headers: Origin, Content-Type, X-Auth-Token');
    header("Content-Type: application/json; charset=UTF-8");

    error_reporting(E_ERROR); 

    include("global.php");

    session_start();
    $userid = ($_SESSION['userid']);
    if (isset($_SESSION['userid']))
        header("Location: login.html");

    $mysqli = new mysqli(Petshop, dbuser, dbpw, db);
    $stmt = $mysqli->prepare("SELECT name FROM users WHERE userid=?");
    $stmt->bind_param("s", $userid); 
    $stmt->execute();
    $stmt->bind_result($name);
    $stmt->fetch();


    echo "<br><br>";
    echo "<h1>". "Welcome, $name!" ."</h1>";
    echo $userid;

    $stmt->close();
    $mysqli->close();


    $mysqli = new mysqli(Petshop, dbuser, dbpw, db);
    $stmt = $mysqli->prepare("SELECT itemid, itemname, category, imagefile, userid FROM items WHERE userid=?");
    $stmt->bind_param("s", $userid); 
    $stmt->execute();
    $stmt->bind_result($itemid, $itemname, $category, $imagefile, $userid);


    while ($stmt->fetch()) {
        echo "<table border='1' style='width:30%'>";
        echo "<td>";
        echo "<b>Item ID: $itemid</b>";
        echo "<br><br>";
        echo "Item Name: <a href ='itemdetails.php?itemid=$itemid'>$itemname</a>";
        echo "<br><br>";
        echo "<img src='images/$imagefile'>";
        echo "<br><br>";
        echo "Category: $category";
        echo "<br><br>";
        echo "Category: $userid";
        echo "<br><br>";
        echo "</td>";


    }
    echo "</table>";
    if ($itemid && $itemname && $imagefile && $category)
    {}
    else {
        $itemid = null; 
        $itemname = null; 
        $imagefile = null; 
        $category = null; 
        echo '<p style="color:red;">No item added under this username</p>';
    }
    echo "<br><br>";
    echo "<a href= 'otherdetails.php'>Other items</a>";
    echo "<br><br>";

    $stmt->close();
    $mysqli->close();

    Please <a href="logout.php">Click Here</a> to logout.

</div>  
<div id="footer">
        Copyright &copy; Happy Pet Shop
    </div>

</body>
</html>

预览:

processlogin.php

intropage.php - before i log out

intropage.php - after i log out

0 个答案:

没有答案