php cookie和会话变量和ip地址 [英] php cookies and session variables and ip address
问题描述
我之前发过一个类似的问题,但从来没有真正得到一个答案帮助我,所以我想再试一次。作为一个免责声明,我知道在这里的很多信息不遵循完美的编码做法,但它仅用于锻炼目的。我试过一百万的东西,没有什么似乎工作,因为我不知道一切都应该去!我非常需要一些(任何!)帮助,所以感谢提前如果你可以提供任何东西!
I posted a similar question before, but never really got an answer that helped me, so I'm looking to try again. As a disclaimer, I know that a lot of the information in here doesn't follow perfect coding practices, but it is for exercise purposes only. I've tried a million things and nothing seems to be working because I'm not really sure where everything should go! I desperately need some (any!) help so thanks in advance if you can offer anything!
我试图创建一个简单的表单/页面使用一些基本的cookie和会话内容以产生一些用户特定的数据。我一直在良好,直到我遇到一些问题,我不能弄清楚。在我的第一页一切都很好,除了我只想要的用户正在使用的浏览器的名称。 (例如,我想只是简单的标题:Firefox而不是整个浏览器的长版本。)我已经看到这样做,所以我认为这是可能的,我只是不知道该怎么做!
I'm trying to create a simple form / page that uses some basic cookie and session stuff to produce some user-specific data. I was moving along good until I came across a few problems that I can’t figure out. On my first page everything is good except for I just want the NAME of the browser the user is using. (for example, I want just the simple title: Firefox instead of the whole long version of the browser.) I've seen this be done so I think it’s possible, I just don’t know how to do it!
我真正的问题在这里出现,因为我不知道如何存储IP地址,浏览器信息和当前日期/时间如第2页所示)作为会话变量。尝试了一些我发现的东西,但我不认为我做的正确。
My real problems come up right about here, because I'm not exactly sure how to store the IP address, browser info and the current date/time (which I want shown on page 2) as session variables. Tried a few things I found, but I don’t think I was doing it right.
我也尝试将用户名和密码存储为两个独立的cookie每个...建议吗?最后,我需要做什么,以具有输出缓冲的位置头(用于调用form_data.php)?
I also worked endlessly on trying to store the username and passwords as two separate cookies each...suggestions? Finally, what do I need to do to have a location header (used to call form_data.php) with output buffering?
(不确定这将是有用的,考虑我可能做了一切错!LOL)这是一个完全缩减版本的我的代码。试图发布我最干净的版本,即使它没有太多的信息,这样你可以很容易地看到我想做什么。
(Not sure this will be that helpful, considering I probably did everything wrong! LOL) This is a totally stripped-down version of my code. Tried to post my cleanest version, even though it doesn't have much info, so that you could easily see what I was trying to do.
主文件代码:
<?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
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 broswer data here...dont 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!";
?>
推荐答案
要获得浏览器, http://us3.php.net/manual/function.get-browser.php =nofollow> get_browser()
function:
To get the browser, use the get_browser()
function:
$browserinfo = get_browser($_SERVER['HTTP_USER_AGENT']);
$browsername = $browserinfo['browser'];
您的会话和Cookie存储将永远不会工作,因为您正在制作 Location);
在尝试设置Cookie之前调用。
Your session and cookie storage will never work because you are making a header("Location");
call before attempting to set cookies. You cannot send any output before setting cookies or establishing a session.
在输出到屏幕之前,请调用 session_start()
;
Before any output to the screen, call session_start()
;
// attach to your session (or create if it doesn't exist)
// You must call session_start() on every page where you intend to access or set session vars
// and it must be called before any output (including whitespace at the top)
session_start();
// Store some stuff...
$_SESSION['ip'] = $_SERVER['REMOTE_ADDR'];
// Store user info in session, not cookie
$_SESSION['username'] = $_POST['username'];
// Set a cookie
// Not a super secure token, but better than user/pass in cookies.
// Point here is just to show that it must be done before any output or before the redirection header.
$_SESSION['token'] = sha1(time() . rand() . $_SERVER['SERVER_NAME']);
setcookie('token', $_SESSION['token']);
// In practice, you'd want to store this token in a database with the username so it's persistent.
// Now do the redirection:
// Supposed to be an absolute URL by the HTTP spec
header("Location: http://example.com/form_data.php");
// exit right after the redirection to prevent further processing.
exit();
在注释后添加ADDENDUM
在工作时,请确保PHP在屏幕上显示所有错误。
While you work, make sure PHP displays all errors on screen. Be sure to turn off display_errors
when your code goes onto a live public server.
error_reporting(E_ALL);
ini_set('display_errors', 1);
要在您的问题中不知道该怎么做, $ _ COOKIE
superglobal:
To retrieve values from cookies as you said in your question you didn't know how to do, use the $_COOKIE
superglobal:
// On the page that sets it...
setcookie('somename', 'somevalue', expiry, domain);
// On the page that retrieves it...
echo $_COOKIE['somename'];
这篇关于php cookie和会话变量和ip地址的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!