0

我正在从我的 first.php 发送会话值并尝试从我的 second.php 获取。我已经对此进行了一些阅读,并且在我的本地主机上运行良好,但在我的服务器上根本无法运行。

这是我的 first.php 文件中的代码:

<?php
session_start();
ob_start();
?>
<!DOCTYPE html>
<html>
    <head>
        <meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
        <link rel="stylesheet" type="text/css" href="http://localhost/Shopping_biblioteka/css/style.css">
        <title></title>
    </head>
    <body align="center">
        <div id="login">
        <?php

        if (isset($_POST['username']) && isset($_POST['password'])) {
            $username = $_POST['username'];
            $password = $_POST['password'];
            mysql_connect("localhost", "shopping_katalog", "logik@112233") or die(mysql_error());
            mysql_select_db("shopping_katalog") or die(mysql_error());
        mysql_query("SET CHARACTER SET utf8");
        mysql_query("SET NAMES utf8");
            $result = mysql_query("SELECT password,id FROM x9qg6_users
            where username='" . $username . "'");
            if (!$result) {
                echo 'Could not run query: ' . mysql_error();
                exit;
            }
            $row = mysql_fetch_row($result);

            $userhash = md5($password . $test[1]);

            if ($test[0] === $userhash) {
                $_SESSION['login_user'] = $user_id;
                $_SESSION['username'] = $username;
                $url = "biblioteka.php";
                header("Location: $url");
            }
        } else {
            echo 'Внесете ги вашите податоци во полињата!';
        }
        ?> 
        <form action="" method="POST" accept-charset="UTF-8"> 
            Корисничко име:<br/>
            <input name="username" id="username" type="text"/><br/>
            Лозинка:<br/>
            <input type="password" id="password" name="password"/><br/>
            <input type="submit" value="Логирај се!"/>
        </form>

        </div>
    </body>
</html> 

这是我的 second.php 文件:

<?php
session_start();

?>
<!DOCTYPE html>
<html>
    <head>
        <meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
        <link rel="stylesheet" type="text/css" href="http://localhost/Shopping_biblioteka/css/style.css">
        <title>Библиотека на</title>
    </head>
    <body>
        <div id="wrapper">
            <?php

            $user_ID = $_SESSION['login_user'];
            $logged_user = $_SESSION['username'];
            ?>
            <h1 align="center" id="b_welcome">Добредојде <?php echo $logged_user; ?> во твојата библиотека! </h1>
            <h4 align="center" id="b_info">Во твојата библиотека ги имаш следниве книги</h4>
            <div id="knigi">
                <?php
                /* OVA E QUERITO
                 * 
                  SELECT *
                  FROM Knigi k, poracki p
                  WHERE k.knigaid = p.kniga
                  AND p.korisnikInt = $user_ID
                 * 
                 */

//-----------------------------------

                if(isset($_SESSION['login_user'])){
                mysql_connect("localhost", "user", "pass) or die(mysql_error());
                mysql_select_db("shopping_katalog") or die(mysql_error());
        mysql_query("SET CHARACTER SET utf8");
        mysql_query("SET NAMES utf8");
                $result = mysql_query("SELECT * FROM Knigi k, poracki p
        WHERE k.knigaid = p.kniga AND p.korisnikInt ='" . $user_ID . "'");
                while ($row = mysql_fetch_array($result)) {

//knigaid,naslov,avtor,link_do_pdf,thumb_link,kategorija,cena,br_strani
                    echo '<div id="item">';
                    echo '<a href="http://' . $row['link_do_pdf'] . '"><h5 align="center" id="b_item_title">' . $row['naslov'] . '</h5></a>';
                    //echo '<a href="http://' . $row['link_do_pdf'] . '">' . $row['avtor'] . '</a>';
                    echo '<a href="http://' . $row['link_do_pdf'] . '"><img src="http://' . $row['thumb_link'] . '" id="b_item_slika" /></a>';
                    echo '</div>';

                }

                if (!$result) {
                    echo 'Проблем со добивање на податоците: ' . mysql_error();
                    exit;
                }
                }else{
                    $url = "/index.php";
                header("Location: $url");
                }
                ?>
            </div><!--kraj na knigite-->
        </div>
    </body>
</html>
4

2 回答 2

0

要检查是否启用了 cookie,请使用代码...

 <?php
setcookie('test', 1, time()+3600);
if(!isset($_GET['cookies'])){
    header('Location:/info.php?cookies=true');
}
if(count($_COOKIE) > 0){
    echo "Cookies are Enabled!";
} else {
    echo "Disabled";
}
?>
于 2012-12-06T12:14:06.480 回答
0

这是概念:

在你first_page.php的用户应该输入他的用户名和密码,如果你发现它们都是正确的并且与数据库中存在的匹配,那么你将设置一个会话并在其中存储你想要的任何数据,然后你会将用户重定向到second_page.phpwhich将使用这些会话存储的值对它们做任何你想做的事情。如果用户没有输入他的用户名和密码,他将留在first_page.php

这是一个简单的示例,您可以从中获取主要概念并将其应用于您的案例:

在 first_page.php

<?php
session_start();
ob_start();
?>
<!DOCTYPE html>
<html>
    <head>
        <meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
        <title></title>
    </head>
    <body align="center">
        <div>
            <?php
                if (!empty($_POST["username"]) && !empty($_POST["password"])) {
                    $_SESSION['username'] = $_POST["username"];
                    header("Location: second_page.php");
                }
            ?> 
            <form action="first_page.php" method="post" accept-charset="UTF-8"> 
                Username: <input type="text" name="username" /><br/>
                Password: <input type="password" name="password"/><br/>
                <input type="submit" value="Login"/>
            </form>
        </div>
    </body>
</html>

而在 second_page.php

<?php
session_start();
?>
<!DOCTYPE html>
<html>
    <head>
        <meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
        <title></title>
    </head>
    <body>
        <h3>Welcome <?php echo $_SESSION["username"] ?></h3>
    </body>
</html>
于 2012-12-06T12:49:10.043 回答