-1

没有饼干,我有点迷失了。我想创建一个通过 url 传递 SID 的会话,但我不知道如何从另一个页面传递和获取数据。我用谷歌搜索了很多,但 90% 的示例都与 cookie 相关。

这就是我所拥有的。

索引.php

<?php
    ini_set("session.use_cookies",0);
    ini_set("session.use_only_cookies",0);
    ini_set("session.use_trans_sid",1);
    session_start();
?>

<html>
<head>
    <title>Index</title>
    <meta http-equiv="content-type" content="text/html;charset=utf-8" />        
    <link rel="STYLESHEET" type="text/css" href="style.css">
</head>
<body>  
    <a href="index.php?" class="navactive">Index</a>
    <a href="second.php?">Second</a>        

    <form action="access.php" method="POST">
        User: <input type="text" name="user" size="15"/><br>
        Pass: <input type="password" name="pass" size="15"/><br>
        <button type="submit"/>Ok</button>
        <button type="reset"/>Reset</button>
    </form>     

    Logged as: <?php print $_SESSION["name"]; ?>                

</body>
</html>

access.php 最后一部分

........
.......
....

    if($count==1){

    // Register $myusername and redirect to file "second.php"
    ini_set("session.use_cookies",0);
    ini_set("session.use_only_cookies",0);
    ini_set("session.use_trans_sid",1);
    session_name('test');

    session_start();
    $_SESSION['name'] = $myusername; 

    header("location:second.php?".SID);
    exit;
}
else {
    echo "Wrong Username";
}
ob_end_flush();
?>

第二个.php

<?php
    ini_set("session.use_cookies",0);
    ini_set("session.use_only_cookies",0);
    ini_set("session.use_trans_sid",1);
    session_start();
?>

<html>
<head>
    <title>Second</title>
    <meta http-equiv="content-type" content="text/html;charset=utf-8" />        
    <link rel="STYLESHEET" type="text/css" href="style.css">
</head>
<body>
    <a href="index.php?">Index</a>
    <a href="second.php?" class="navactive">Second</a><br>              

    <a href="logout.php">Logout</a><br>         

    Logged as: <?php print $_SESSION["name"]; ?>                    

</body>
</html>

注销.php

<?php
    ini_set("session.use_cookies",0);
    ini_set("session.use_only_cookies",0);
    ini_set("session.use_trans_sid",1);
    session_start();
    session_unset();    
    session_destroy();
    header('Location: index.php');
    exit;
?>

- 我必须在“登录为:”中输入什么?"打印 $_SESSION["name"];" 什么都不显示。

-当我登录时,我被重定向到 second.php,然后我单击任何链接,实际记录的会话终止并且 SID 更改。

谢谢!

4

2 回答 2

4

我只是复制了您的代码并自己进行了测试。一切都很好,但不要在 access.php 文件中使用 session_name('test') 。不太确定那是做什么的,但是当我包含它时它会中断。相反,我使用 $_SESSION['name'] 而不调用 session_name() 函数并且一切正常。

于 2012-12-26T00:44:29.203 回答
0

为了通过 URL 传递任何内容,您必须使用正确的语法:

your.url.com/ ?key=value&key2=value2...等等

然后检索此数据:

echo $_GET['key']echo $_GET['key2']

于 2012-12-26T00:38:07.273 回答