gpt4 book ai didi

php cookie 和 session 变量和 ip 地址

转载 作者:可可西里 更新时间:2023-11-01 13:48:18 26 4
gpt4 key购买 nike

我之前发布过类似的问题,但从未真正得到对我有帮助的答案,所以我想再试一次。作为免责声明,我知道此处的许多信息并未遵循完美的编码实践,但仅供练习之用。我已经尝试了一百万件事,但似乎没有任何效果,因为我不确定一切应该去哪里!我迫切需要一些(任何!)帮助,如果您能提供任何帮助,请提前致谢!

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

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

我还不断尝试将用户名和密码分别存储为两个单独的 cookie...有什么建议吗?最后,我需要做什么才能使位置 header (用于调用 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\" />";
?>

表单数据.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'];

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

在任何输出到屏幕之前,调用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 在屏幕上显示所有错误。当您的代码进入实时公共(public)服务器时,请务必关闭 display_errors

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

如您在问题中所说您不知道如何从 cookie 中检索值,请使用 $_COOKIE 超全局:

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

// On the page that retrieves it...
echo $_COOKIE['somename'];

关于php cookie 和 session 变量和 ip 地址,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/6500654/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com