我想创建一个简单的表单/页面,它使用一些基本的cookie和会话内容来生成一些用户特定的数据。直到遇到一些我无法解决的问题时,我才开始顺利进行。在我的第一页上,除了我只想要用户使用的浏览器的名称以外,一切都很好。 (例如,我只想简单的标题:Firefox而不是整个浏览器的长版本)。我已经看到这样做,所以我认为这是可能的,我只是不知道该怎么做!php cookies和会话变量表格
我的真正问题在这里出现,因为我不确定如何将IP地址,浏览器信息和当前日期/时间(我希望在第2页上显示)存储为会话变量。尝试了我发现的一些事情,但我认为我没有做对。
我也试图将用户名和密码存储为两个单独的Cookie,每个都有无尽的建议?最后,我需要做什么来使用输出缓冲来获取位置标题(用于调用form_data.php)?我觉得我一直在关注cookies /会话,直到我真正尝试使用它们!你可以告诉我,我是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();'代码顶部的第一个东西,你绝对不会写任何这样的会话数据。 – jerluc
啊,是的。甚至没有意识到...看到我只见过其他的例子,我正在尝试做什么......从来没有写过这样的东西,所以我有点亏了,试图找出什么去了在哪里,等等更多的建议/例子? – Clark
你读过http://us.php.net/manual/en/book.session.php吗? – BugFinder