简体   繁体   English

如何将变量传递给会话而不是URL-PHP?

[英]How to pass variables to Sessions rather than to the URL - PHP?

<a class='okok' id='$file' href='" . $_SERVER['PHP_SELF'] . "?file=" . $file . "'>$file</a>

The above code is a link which passes its name to the variable 'file' which then gets displayed in the URL as: 上面的代码是一个链接,该链接将其名称传递给变量“文件”,然后该变量在URL中显示为:

http://example.com?file=thefile.html (whatever the file is) http://example.com?file=thefile.html (无论文件是什么)

Using PHP, I can now retrieve the variable from the URL and then process it (in this case the variable is 'thefile.html'. 现在,使用PHP,我可以从URL中检索变量,然后对其进行处理(在这种情况下,变量为'thefile.html'。

How would I use Sessions to prevent variables from getting passed onto the URL. 我将如何使用会话来防止变量传递到URL上。 So instead of: 所以代替:

http://example.com?file=thefile.html

I would have: 我会:

http://example.com

And the variable 'thefile.html' would be stored in a Session. 并且变量“ thefile.html”将存储在会话中。

If you need more info on what I am trying to ask, then please ask. 如果您需要有关我要询问的更多信息,请询问。

You've got to create a valid page for receiving the query string you're passing in the url suppose the link you have is on index.html and your link destination is result.php, then you'd write the following code in result.php 您必须创建一个有效的页面来接收在URL中传递的查询字符串,假设您所拥有的链接位于index.html上,并且链接目标是result.php,那么您需要在结果中编写以下代码.PHP

<?php
session_start();
if( isset($_GET["filename"]) ){
$fname = $_GET["filename"];
//code to deal with filename 

//save in session variable 
$_SESSION['UploadedFilename'] = $fname;
}
?>
<!-- html -->
<?php
session_write_close();
?>

If you wanted to pass the file name from the browser (where the link is) to the host without a parameter, you would need to use a cookie. 如果要不带参数将文件名从浏览器(链接所在的位置)传递给主机,则需要使用cookie。 Sessions are a host concept. 会话是一个主机概念。

So, as a simple example, you could use: 因此,作为一个简单的示例,您可以使用:

<a class='okok' id='$file' href='" . $_SERVER['PHP_SELF'] . "' onclick="document.cookie='filename=" . $file . "';">$file</a>

This creates a cookie with the filename in it. 这将创建一个包含文件名的cookie。

On the host side, you retrieve the name with $_COOKIES['filename']. 在主机端,您可以使用$ _COOKIES ['filename']检索名称。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM