【发布时间】:2011-06-27 07:19:46
【问题描述】:
我正在尝试创建一个简单的表单/页面,它使用一些基本的 cookie 和会话内容来生成一些用户特定的数据。在遇到一些我无法弄清楚的问题之前,我一直进展顺利。在我的第一页上,一切都很好,除了我只想要用户正在使用的浏览器的名称。 (例如,我只想要一个简单的标题:Firefox 而不是整个长版浏览器。)我已经看到这样做了,所以我认为这是可能的,我只是不知道该怎么做!
我真正的问题出现在这里,因为我不确定如何将 IP 地址、浏览器信息和当前日期/时间(我希望在第 2 页显示)存储为会话变量。尝试了一些我发现的东西,但我认为我做得不对。
我还无休止地尝试将用户名和密码分别存储为两个单独的 cookie...建议?最后,我需要做什么才能有一个带输出缓冲的位置标头(用于调用 form_data.php)?我觉得我一直在关注 cookie/会话,直到我真正尝试使用它们!
如您所知,我是 PHP 新手(但不是一般的编程),所以虽然我对其中的一些有所了解,但我正在努力学习(并掌握)PHP 的独特品质!如果您能提供任何帮助,我将不胜感激!
代码时间! (不确定这是否会有帮助,考虑到我可能做错了一切!大声笑)这是我代码的完全精简版本。试图发布我最干净的版本,即使它没有太多信息,这样你就可以很容易地看到我想要做什么。
主文件代码:
<?php
header('Location: form_data.php');
setcookie('username', $_POST['username']);
setcookie('password', $_POST['password']);
//I know this isn't working.
//honestly I just left this in here as to show where I had been
//trying to save the cookie data. Pretty obvious how bad my
//trial and error with this went!
}
?>
<?php
$_SESSION['ip'] = $_SERVER['REMOTE_ADDR'];
echo " By the way, your IP address is: </b>".$_SESSION['ip']."<br />";
echo " You already know this, but the browser you are currently using
to view this page is:<br/>"; //What is the correct function that I should be using here?
echo "<form action=\"form_data.php\" method=\"post\">";
echo "username:<input type=\"text\" name=\"username\" size=\"20\" value=\"\"><br/>";
echo "password:<input type=\"password\" name=\"password\" size=\"20\" value=\"\"><br/>";
echo "<input type=\"submit\" value=\"Submit, please\" />";
echo "<br /><input type=\"hidden\" name=\"submitted\" value=\"true\" />";
?>
form_data.php:
<?php
echo "Hello, ".$username;//I'm trying to get the cookie data for the username
echo "Your password is ".$password; //Samething here (want cookie data)
echo "The date and time you entered this form is: ".date("F j, Y")." -- ".date("g:i a");
echo "<br/>Your IP:".$_SESSION['ip'] = $_SERVER['REMOTE_ADDR'];
echo "<br/>Your broswer:".;//I want full browser data here... don't know how to do it.
//Overall, was this the way to get the session variables for IP, date/time and browser?
echo "Thank you for filling out this form!";
?>
【问题讨论】:
-
如果不首先在代码顶部调用
session_start();,您绝对不会编写任何这样的会话数据。 -
啊,是的。甚至没有意识到......看我只见过我正在尝试做的其他例子......我自己从来没有写过这样的东西所以我有点茫然试图弄清楚发生了什么在哪里等等。还有什么建议/例子吗?
-
没有任何充分的理由将密码存储在 cookie 中。将用户名存储在 cookie 中几乎一样糟糕
标签: php forms session variables cookies