php cookie和会话变量以及ip地址


php cookies and session variables and ip address

我之前发布过一个类似的问题,但从未得到真正帮助我的答案,所以我想再试一次。作为免责声明,我知道这里的很多信息都没有遵循完美的编码实践,但这只是为了练习。我已经尝试了一百万种方法,但似乎都不起作用,因为我真的不确定一切都应该去哪里!我迫切需要一些(任何!)帮助,所以如果你能提供任何帮助,请提前感谢!

我正在尝试创建一个简单的表单/页面,它使用一些基本的cookie和会话内容来生成一些特定于用户的数据。我一直过得很好,直到遇到了一些我无法解决的问题。在我的第一个页面上,除了我只想要用户正在使用的浏览器的名称之外,一切都很好。(例如,我只想要一个简单的标题:Firefox,而不是整个长版本的浏览器。)我见过这样做,所以我认为这是可能的,我只是不知道如何做到!

我真正的问题就出现在这里,因为我不确定如何将IP地址、浏览器信息和当前日期/时间(我希望在第2页显示)存储为会话变量。尝试了一些我发现的东西,但我认为我做得不对。

我还无休止地尝试将用户名和密码分别存储为两个独立的cookie。。。建议?最后,我需要做些什么才能有一个带有输出缓冲的位置头(用于调用form_data.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 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!"; 
   ?> 

要获取浏览器,请使用get_browser()函数:

$browserinfo = get_browser($_SERVER['HTTP_USER_AGENT']);
$browsername = $browserinfo['browser'];

您的会话和cookie存储将永远无法工作,因为您在尝试设置cookie之前正在进行header("Location");调用。在设置cookie或建立会话之前,您不能发送任何输出。

在任何输出到屏幕之前,调用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();

评论后的附录

工作时,请确保PHP在屏幕上显示所有错误。当代码进入实时公共服务器时,请确保关闭display_errors

error_reporting(E_ALL);
ini_set('display_errors', 1);

要像您在问题中所说的那样从cookie中检索值,您不知道如何操作,请使用$_COOKIE superglobal:

// On the page that sets it...
setcookie('somename', 'somevalue', expiry, domain);
// On the page that retrieves it...
echo $_COOKIE['somename'];
> I'm trying to create a simple form /
> page that uses some basic cookie and
> session stuff to produce some
> user-specific data.

会话确实使用了隐藏的cookie(只将session_id存储在cookie/set_cookie中),我建议您只使用会话,因为cookie可能会泄露信息(将cookie中的所有信息存储在该用户的计算机上),当会话使用服务器的文件系统/数据库时,或者当您覆盖session_set_save_handler时,这可能是危险的。


> On my first page everything is good
> except for I just want the NAME of the
> browser the user is using.

就像Michael说的那样,你可以使用get_browser:

尝试确定功能用户浏览器的中的浏览器信息browscap.ini文件。

就像PHP页面所说的,它试图确定,而你应该NOT依赖这些信息来做任何重要的事情,因为它可能是错误的(如果你愿意,你可以欺骗系统)。我的意思是,你不应该用它来验证/证明某事。


> 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.

可以在这里阅读更多检索IP地址的信息(代理服务器可能会误导你一点?)。要存储这些信息,只需将其存储在会话中,方法是首先在每个想要使用会话的页面顶部(在输出任何内容之前)发出session_start()(只有那些不在每个页面上设置cookie的页面,这会使页面稍微慢一点),然后按照$_SESSION['time'] = date(DATE_RFC822);的行将当前时间存储在会话变量中。您可以阅读更多关于在date()页面检索时间的信息。

所以page 1上的代码看起来像:

<?php
session_start();
$_SESSION['ip'] = getRealIpAddr(); # no php function => See http://roshanbh.com.np/2007/12/getting-real-ip-address-in-php.html
$_SESSION['time'] = date(DATE_RFC822);

然后在page 2上,您可以使用以下内容检索这些信息:

<?php
session_start();
echo $_SESSION['ip']; // retrieve IP
> I also worked endlessly on trying to
> store the username and passwords as
> two separate cookies
> each...suggestions?

不要将它们存储在cookie中(仅使用set_cookie,而不使用会话来存储信息),而是将其存储在会话中以提高安全性。但是会话也容易受到会话固定的影响,因此在会话中存储了一些关键信息后,您应该重新生成会话id,并且永远不要向浏览器/用户输出/显示该信息,以防止任何泄漏。


> Finally, what do I need to do to have
> a location header (used to call
> form_data.php) with output buffering?

就像Michael说的那样,在之后,您应该使用头函数和出口来终止脚本

<?php
header("Location: http://www.example.com/"); /* Redirect browser */
/* Make sure that code below does not get executed when we redirect. */
exit;
?>

附言:永远不要在自己的数据库中存储任何真正敏感的信息,比如信用卡(使用贝宝或其他什么)号码或任何东西。我还建议你不要将密码存储在数据库中,而是使用openId(谷歌的)之类的东西来处理你的身份验证,以获得额外的安全性