在PHP中设置cookie,然后重定向到新的URL并访问设置的cookie?
我试图设置一个cookie,然后重定向到一个新的URL并访问(或检查是否设置了cookie)设置的cookie,但它似乎在新的URL中不可用。
<?php
header("Location: http://www.facebook.com/pages/tabappURL");
setcookie('coupon', true, time() + 120); //hold for 2 min, time in seconds
?>
有什么想法吗?
另外,让我解释一下这是什么,或者为什么我要尝试这样做。我有一个 Facebook 选项卡应用程序,它是一个 iframe...用户喜欢该页面,然后会看到一个竞赛报名表(由 Wufoo 提供支持)。用户提交表单后,wufoo 会重定向到感谢页面……不过,它会刷新并让您离开 Facebook。这不是理想的情况,因为我们希望感谢页面驻留在 iframe 中。我无法控制 wufoos 端的 iframe 目标,因此解决方法是重定向到我的服务器上的另一个页面(托管 iframe 中内容的同一服务器)设置一个 cookie,然后重定向回 FB 应用程序页面。然后 FB 应用程序检查 cookie 是否已设置,然后显示感谢消息。
从技术上讲,这是两个页面:
index.php(其中有)
<div class="not-liked">please like us</div>
<div class="liked">enter contest</div>
<div class="thanks">thank you</div> <!-- hidden until page refresh and cookie set -->
redirect.php(其中有上面的 cookie/重定向代码)
Im trying to set a cookie, then redirect to a new URL and access( or check if the cookie is set) the set cookie, but it doesn't seem to be available in the new URL.
<?php
header("Location: http://www.facebook.com/pages/tabappURL");
setcookie('coupon', true, time() + 120); //hold for 2 min, time in seconds
?>
Any ideas?
Also, let me explain what is, or why Im trying to do this. I have a Facebook tab app, which is an iframe... A user likes the page, then is presented with a contest entry form (powered by Wufoo). Once the user submits the form, wufoo redirects to a thank you page... though, it refreshes and takes you away from Facebook. This wasnt the ideal situation since we want the thank you page to reside within the iframe. I had no control of targeting the iframe on wufoos end so the work around would be to redirect to another page on my server (same server that is hosting the content in the iframe) set a cookie, then redirect back to the FB app page. Then the FB app checks if the cookie is set, then displays the thank you message.
Technically this is two pages:
index.php (which has)
<div class="not-liked">please like us</div>
<div class="liked">enter contest</div>
<div class="thanks">thank you</div> <!-- hidden until page refresh and cookie set -->
redirect.php (which has the cookie/redirect code above)
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
Cookie 通常只能在指定域内访问。
来自 PHP 文档:
Cookies are typically only accessible within a specified domain.
From the PHP documentation:
检查您的浏览器设置,看看您是否启用了第三方 cookie。在某些浏览器中,如果禁用它们,iframe 内的页面将不会接收 cookie,即使它们属于同一域。
Check your browser settings to see if you have enabled third-party cookies. In some browsers, if they are disabled a page inside an iframe will not receive cookies even if they belong to the same domain.
你调用了session_start()吗?这将使 PHP 检查会话 cookie 的请求标头。
Did you call session_start()? That will get PHP to check the request headers for the session cookie.