在 php cgi 环境中设置 cookie,而不使用 php api

发布于 2024-07-10 09:04:37 字数 53 浏览 5 评论 0原文

如何在 php CGI 环境中使用 cookie,而不使用 PHP 中的任何 api 函数?

How can I use cookies in a php CGI Enviroment without using any api functions from PHP?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

人海汹涌 2024-07-17 09:04:37

好的,您想在 CGI 环境中使用 php 设置 cookie 吗?

复制代码并将其命名为whatever.cgi并使其可执行。

#!/usr/bin/php
function set_cookie($cookiename,$cookievalue,$cookietime){
   echo 'set-cookie: '.$cookiename.'="'.$cookievalue.'"; max-age="'.$cookietime.'";'."\n";  
}

行尾的\n是必须的。 否则你会遇到麻烦:)
现在让我们设置一个 cookie:

set_cookie("foo","bar",60)

将名称为 foo 的 Cookie 设置为值栏。 60 秒后过期。

现在您可以从 HTML 标头开始。

echo "Content-Type: text/html\n\n";
echo "<html>\n";
echo "<head>\n";
echo "<title>whatever</title>\n";
echo "</head>\n";
echo "<body>\n";

如果要删除 cookie,请将 max-age 设置为零

function set_cookie($cookiename){
   echo 'set-cookie: '.$cookiename.'="0"; max-age="0";'."\n";  
}

Ok you want to setup cookies with php in a CGI Enviroment?

Copy the code and name it to whatever.cgi and make it executable

#!/usr/bin/php
function set_cookie($cookiename,$cookievalue,$cookietime){
   echo 'set-cookie: '.$cookiename.'="'.$cookievalue.'"; max-age="'.$cookietime.'";'."\n";  
}

The \n at the end of the line is a must. Or you will run into trouble :)
Now let's set a cookie:

set_cookie("foo","bar",60)

Sets the Cookie with the Name foo to the Value bar. Expires in 60 seconds.

Now you can start with the HTML Header.

echo "Content-Type: text/html\n\n";
echo "<html>\n";
echo "<head>\n";
echo "<title>whatever</title>\n";
echo "</head>\n";
echo "<body>\n";

If you want to delete the cookie, set the max-age to zero

function set_cookie($cookiename){
   echo 'set-cookie: '.$cookiename.'="0"; max-age="0";'."\n";  
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文