用php无法获取会话值

时间:2012-12-06 12:00:37

标签: php html session

我正在从我的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>

2 个答案:

答案 0 :(得分: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";
}
?>

答案 1 :(得分:0)

这是概念:

在你first_page.php中,用户应该输入他的用户名和密码,如果你发现它们都是正确的并且与数据库中存在的那些相匹配,那么你将设置一个会话并存储你想要的任何数据,然后您将用户重定向到second_page.phpfirst_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>

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

在first_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>

在second_page.php中

{{1}}