2012-12-26 61 views
-1

我有点失去了没有cookie的工作。 我想创建一个通过URL传递SID的会话,但我不知道如何传递并从另一个页面获取数据。 我google了很多,但90%的例子都是用cookies。没有cookie的PHP会话

这是我的。

的index.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(); 
?> 

second.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> 

logout.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; 
?> 

- 我必须在“记录为:”中输入什么? “print $ _SESSION [”name“];”什么都没显示

- 当我登录时,我被重定向到second.php,然后我点击任何链接,实际记录的会话死亡和SID更改。

thx!

+3

Cookie是传递会话令牌的明智方式。你为什么试图避免它们? – Quentin

+0

使用“session.use_trans_sid”会导致谷歌排名低你的网站。如果可能的话,你不应该使用这个。 “$ _SESSION [”name“]”应该是“$ _SESSION ['name']”...单引号 –

+0

@Quentin我知道,但它是一个大学的练习,我不能使用cookies :( – krovs

回答

4

我只是复制你的代码,并测试了它自己。一切都很好,但不要在你的access.php文件中使用session_name('test')。我不确定那是干什么的,但是当我把它包括在内时,它会破坏。相反,我使用$ _SESSION ['name']而不调用session_name()函数,并且所有工作都正常。

+0

根据[文档](http://php.net/manual/en/function.session-name.php):“你需要为每个请求调用session_name() (并且在调用session_start()或session_register()之前)“,所以它需要在所有页面上调用,而不仅仅是在access.php – jswolf19

+2

我相信这意味着,如果使用会话名称,则需要在所有页面上调用它你的网页,不只是在你的access.php上,我建议不要使用它,而不是你的应用程序是什么,但只是删除它,一切都会工作。 – uptownhr

+0

@JamesLee是啊!session_name()是问题! 现在它工作! thx – krovs

0

为了通过URL来传递任何你必须使用正确的语法:

your.url.com/ 键=值&键2 =值...

然后检索此数据:

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