2012-02-11 54 views
0

即时通讯开始学习php,一个小型应用程序即时通讯大厦需要根据用户是否有首选cookie将访问者发送到特定页面,如果是,则根据哪个cookie转到特定页面。基于Cookie重定向访问者

所以在我的应用程序的访问者将面临三个环节,当访问者点击一个链接的一个部分一个cookie remebering游客将优先发送,从而

Link1 = Prefcookie1 
Link2 = Prefcookie2 
Link3 = Prefcookie3 

它需要

另一页

检查任何prefcookies 如果没有重定向到index1

如果是检查哪些precookie

If prefcookie = prefcookie1 redirect to index 
if prefcookie = prefcookie2 redirect to index1 
if precookie = prefcookie3 redirect to index3 

或者可以通过htaccess实现类似的功能吗?

任何帮助非常感谢,它是过去的事情来完成这个

回答

0

PHP将工作的优良这样的:

<? 
if($_COOKIE['prefcookie'] == "prefcookie1") 
{header("Location: index");} 
elseif($_COOKIE['prefcookie'] == "prefcookie2") 
{header("Location: index2");} 
elseif($_COOKIE['prefcookie'] == "prefcookie3") 
{header("Location: index3");} 
else 
{header("Location: index1");} 
?> 

左右

+0

谢谢你,我一直在开发这个应用程序的一部分时使用这段代码,当我直接去检查cookie文件时,它按照计划工作,并基于它们的优先权重新提供一个用户,当它完成它的所有时间并把这个代码到索引文件,它不会重定向人,因为它应该和加载一个空白屏幕 – 2012-02-23 18:42:49

0

您可以在PHP读取cookie中看到http://php.net/manual/en/features.cookies.php

但你提到的htaccess这表明你打算在Apache中,的RewriteRules这样做哪些与PHP无关。如果你可以用RewriteRules做到这一点,答案是肯定的,尽管你需要一个支持它的服务器。见http://httpd.apache.org/docs/2.0/mod/mod_rewrite.html(在服务器变量部分)

+0

我只提到htaccess的,因为它输入问题时,提出了一个建议的答案,我喜欢尝试做到这一点用PHP,以避免兼容性问题 – 2012-02-11 06:58:12

0

您可以将“检查脚本”您要重定向所有页面。插入第一行。

例如:通过“thefinn93”

<? 
if($_COOKIE['prefcookie'] == "prefcookie1") 
{header("Location: index");} 
elseif($_COOKIE['prefcookie'] == "prefcookie2") 
{header("Location: index2");} 
elseif($_COOKIE['prefcookie'] == "prefcookie3") 
{header("Location: index3");} 
else 
{header("Location: index1");} 
?> 

SD

+0

感谢你的提示,我一直在开发这个应用程序的一部分时使用这段代码,当我直接去检查cookie文件时,它按照计划工作,并根据他们的使用者重新提供一个用户来完成它,代码到索引文件,它不会重定向人,因为它应该加载一个空白屏幕 – 2012-02-23 19:45:56