[英]Are there any domain specific global variables in PHP?
我有 2 个网站在我的本地主机上运行
两个网站的初始代码是:
if(isset($_SESSION['user'])){
header("location:home.php")
}
// This code redirects index.php to home.php if the session already exists
.........next lines are login code..........
//
//
//
............................................
我已经使用 index.php 中的登录表单成功登录到 sample_website1,并且 session 已激活。 $_SESSION['user']=$username
现在我打开了一个新选项卡并访问了 sample_website2,它在其中使用 session 变量 $_SESSION['user'] 已在 sample_website1 中设置并自动登录到 home.php 从 index.ZE1634FD7623EZCEE409CEECEE409
我需要一个 session 变量,该变量仅在 sample_website1 中有效,这样它就不会直接在其他网站(如 sample_website2)上对用户进行身份验证。
使用session_name("sample_websiteN");
在session_start()
之前(将 N 替换为 1 或 2),请参阅 php.net/session_name
在 localhost/sample_website1/index.php
<?php
session_name("sample_website1");
session_start();
if(isset($_SESSION['user'])){
header("location:home.php")
}
// This code redirects index.php to home.php if the session already exists
.........next lines are login code..........
//
//
//
............................................
在 localhost/sample_website2/index.php
<?php
session_name("sample_website2");
session_start();
if(isset($_SESSION['user'])){
header("location:home.php")
}
// This code redirects index.php to home.php if the session already exists
.........next lines are login code..........
//
//
//
............................................
来自 php.net 的解释: The session name references the name of the session, which is used in cookies and URLs (eg PHPSESSID)
使用此解决方案,您可以在相同域和不同域中以完全相同的行为部署代码。 You can host both directory in localhost/sample_website1/index.php and localhost/sample_website2/index.php OR in sample_website1.com/index.php and sample_website2.com/index.php with same code
如果 session 名称很难硬编码,则将 session 名称字符串保存在配置文件中的某个位置
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.