如果 URL 中没有文件名,$_SESSION 将无法工作?
我目前正在一个网站上工作,我正在尝试使用会话变量。
我有一个以 session_start();
开头的控制器脚本 (index.php),并且在 if
语句中包含两个不同的 HTML 文件。当我转到 /quote/index.php
时,一切正常,我设置的会话变量会按预期在页面上回显,但是如果我删除 'index.php< /code>*' 来自 URL,因此它仅指向页面加载的
/quote
,但没有显示任何会话变量。
我没有在脚本中的任何地方使用 session_destroy
并且会话变量没有回显“0
”,所以我相当确定它们没有被取消设置,看起来就好像它们在 URL 中没有文件名的情况下被忽略一样!
任何关于为什么会发生这种情况的见解都会很棒, 谢谢
/quote/index.php(删除了无关的位):
<?php
session_start();
if (isset($_GET['form']))
{
include 'form.html.php';
exit();
}
if (isset($_GET['fetchquote']))
{
$width = mysqli_real_escape_string($link, $_POST['width']);
$height = mysqli_real_escape_string($link, $_POST['height']);
$_SESSION['height'] = $height;
$_SESSION['width'] = $width;
}
include 'quote.html.php';
?>
会话变量在 quote.html.php 中得到回显
I'm currently working on a site where I'm trying to make use of the session variables.
I have a controller script (index.php) that begins with session_start();
and has two different HTML files included within if
statements. Everything works all groovy when I go to /quote/index.php
, the session variables that I've set are echoed on the page as expected, however if I remove 'index.php
*' from the URL so it points to just /quote
the page loads however none of the session variables show up.
I'm not using session_destroy
anywhere in my scripts and the session variables aren't echoing '0
' so I'm fairly sure they aren't being unset, it seems as though they are just ignored without the filename in the URL!
Any insight as to why this is occuring would be awesome,
Thanks
/quote/index.php (with extraneous bits removed):
<?php
session_start();
if (isset($_GET['form']))
{
include 'form.html.php';
exit();
}
if (isset($_GET['fetchquote']))
{
$width = mysqli_real_escape_string($link, $_POST['width']);
$height = mysqli_real_escape_string($link, $_POST['height']);
$_SESSION['height'] = $height;
$_SESSION['width'] = $width;
}
include 'quote.html.php';
?>
The session variables are echoed in quote.html.php
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
这两个文件名是什么?
似乎您包含的文件之一名为index.html,并且驻留在 mysite.com/quote/ 本身中。如果我没记错的话,如果目录中有index.html和index.php,那么默认情况下会加载index.html,除非在url中明确指定该文件。所以在你的情况下,当你没有明确指定index.php时,index.html就会被加载。当然,只有当目录中有一个index.html时才会出现这种情况。
what are the two file names?
seems that one of the file that you are including is named index.html and resides in the mysite.com/quote/ itself. And if I am not wrong, if in a directory there are index.html and index.php then the index.html is loaded by default unless the file is explicitly specified in the url. So it seems in your case when you are not specifying the index.php explicitly,the index.html is being loaded.Of course this is the case only if there is an index.html there in the directory.
确保您还在要回显会话变量的 PHP 页面顶部使用
session_start()
。并确保 index.php 是根目录中的唯一索引。您正在检查是否通过 GET 方法设置了选项。使用 GET 方法的表单在哪里?
发布你的整个脚本,你会得到更好的答案。
Make sure you are also using
session_start()
at the top of the PHP pages where you want to echo the session variable. And make sure index.php is the only index in your root.You are checking if an option is set via the GET method. Where is your form using the GET method?
Post your entire script and you'll get much better answers.
这可能与会话 ID cookie 的有效范围有关。因为如果 cookie 路径 设置为 < code>/quote/,cookie 仅在
/quote/
及以后可用。This has probably something to do with the validity scope of the session ID cookie. Because if the cookie path is set to
/quote/
, the cookie will only be available in/quote/
and beyond.