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

clk*_*clk 5 php variables cookies session

我之前发过一个类似的问题,但从来没有真正得到过帮助我的答案,所以我想再试一次.作为免责声明,我知道这里的许多信息并不遵循完美的编码习惯,但它仅用于练习目的.我已经尝试了一百万件事,似乎没有什么工作,因为我不确定应该去哪里!我迫切需要一些(任何!)帮助,所以如果你能提供任何东西,请提前感谢!

我正在尝试创建一个简单的表单/页面,它使用一些基本的cookie和会话内容来生成一些用户特定的数据.我一直在努力,直到遇到一些我无法弄清楚的问题.在我的第一页上一切都很好,除了我只想要用户正在使用的浏览器的名称.(例如,我只想要简单的标题:Firefox而不是浏览器的整个长版本.)我已经看到这样做了所以我认为这是可能的,我只是不知道该怎么做!

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

我还无休止地尝试将用户名和密码存储为两个单独的cookie,每个...建议?最后,我需要做什么才能使用输出缓冲来获得位置标头(用于调用form_data.php)?

(不确定这会有用,考虑到我可能做错了什么!LOL)这是我的代码的完全精简版本.试图发布我最干净的版本,即使它没有太多的信息,所以你可以很容易地看到我想要做的事情.

主文件代码:

<?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\" />"; 
 ?> 
Run Code Online (Sandbox Code Playgroud)

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!"; 
   ?> 
Run Code Online (Sandbox Code Playgroud)

Mic*_*ski 5

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

$browserinfo = get_browser($_SERVER['HTTP_USER_AGENT']);
$browsername = $browserinfo['browser'];
Run Code Online (Sandbox Code Playgroud)

您的会话和cookie存储将永远不会起作用,因为您header("Location");在尝试设置cookie之前正在进行呼叫.在设置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();
Run Code Online (Sandbox Code Playgroud)

评论后的附录

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

error_reporting(E_ALL);
ini_set('display_errors', 1);
Run Code Online (Sandbox Code Playgroud)

要像你在问题中所说的那样从cookie中检索值,你不知道该怎么做,请使用$_COOKIE超全局:

// On the page that sets it...
setcookie('somename', 'somevalue', expiry, domain);

// On the page that retrieves it...
echo $_COOKIE['somename'];
Run Code Online (Sandbox Code Playgroud)