gpt4 book ai didi

php - Codeigniter单站点登录

转载 作者:行者123 更新时间:2023-11-29 14:42:07 25 4
gpt4 key购买 nike

我正在尝试拥有它,因此如果我从网站的一个部分登录,它也会登录到其他部分。

一些注意事项:

  • 所有登录都连接到同一个数据库...我们称之为 db1
  • 站点 1 的表是 phpfox
  • 站点 2 的表格是 vbulletin
  • 现在它成功登录到 phpfox,但我也不知道如何登录论坛......我在下面添加了几行代码来尝试执行此操作,但我不知道如何登录输出:
  • 问题是我必须不断登录网站的每个部分(使用相同的用户名和密码)

这是我当前的代码:

function login($username, $password, $passClean = null)
{

$this->faildLogins = new DB_FaildLogins();

$ip = $this->input->ip_address();

$faildLogins = $this->faildLogins->getFaildLoginsByIp($ip);

if($faildLogins){
if($faildLogins->bannedTime > 0){
$timeElapsed = ($faildLogins->lastTryDate + $faildLogins->bannedTime)-time();
if($timeElapsed > 0){
return sprintf('Your ip (%s) was banned for %s please try again after expire ban time!', $this->input->ip_address(), seconds2HumanTimeFormat($timeElapsed));
}
}
}

$result = $this->user_model->get_login_info($username);

if ($result) {
if ($result->status == 'pending') {
return 'INACTIVE';
}

if ($result->status == 'rejected') {
return 'REJECTED';
}


if ($password === $result->password) {
$this->CI->session->set_userdata(array('id'=> $result->id));

$this->user_model->addUserLogin($result->id);

$faildLogins = $this->faildLogins->getFaildLoginsByIp($ip);
if($faildLogins){
$this->faildLogins->resetFaildLoginToIp($ip);
}
return TRUE;

// If passwords don't match
} else {

@mysql_connect('localhost', 'db1', 'db1_password') or die ("Can't connect to DB!");
@mysql_connect('localhost', 'db1', 'db1_password', true) or die ("Can't connect to DB!");
@mysql_select_db('phpfox') or die ("Can't select DB!");
@mysql_select_db('vbulletin') or die ("Can't select DB!");

$phpFoxUser = mysql_fetch_array(mysql_query("SELECT * FROM `phpfox_user` WHERE `user_name` = '{$username}'"), MYSQL_ASSOC);

if($phpFoxUser['user_name'] == $username AND
$phpFoxUser['email'] == $result->email AND
md5(md5($passClean).md5($phpFoxUser['password_salt'])) == $phpFoxUser['password']) {

$DBUsers = new DB_Users();
$rows['id'] = $result->id;
$rows['password'] = md5($passClean);

if($DBUsers->saveIt($rows)) {
$this->CI->session->set_userdata(array('id'=> $result->id));
return TRUE;
} else {
$this->faildLogins->addFaildLoginToIp($ip);
return FALSE;
}
} else {
$this->faildLogins->addFaildLoginToIp($ip);
return FALSE;
}
}

} else {
@mysql_connect('localhost', 'db1', 'db1_password') or die ("Can't connect to DB!");
@mysql_connect('localhost', 'db1', 'db1_password', true) or die ("Can't connect to DB!");
@mysql_select_db('phpfox') or die ("Can't select DB!");
@mysql_select_db('vbulletin') or die ("Can't select DB!");

$result = mysql_query("SELECT * FROM `phpfox_user` WHERE `user_name` = '{$username}'");
$phpFoxUser = mysql_fetch_array($result, MYSQL_ASSOC);

if($phpFoxUser['user_name'] == $username AND md5(md5($passClean).md5($phpFoxUser['password_salt'])) == $phpFoxUser['password']) {

$DBUsers = new DB_Users();
$rows['username'] = $phpFoxUser['user_name'];
$rows['password'] = md5($passClean);
$rows['usergroup'] = 'user';
$rows['email'] = $phpFoxUser['email'];
$rows['activationCode'] = md5(time());
$rows['status'] = 'approved';
$rows['registerDate'] = time();
$rows['registerIp'] = $this->input->ip_address();
$rows['hash'] = uniqid(rand().rand().rand(), true);

$newUserId = $DBUsers->saveIt($rows);

if($newUserId) {
$this->CI->session->set_userdata(array('id'=> $newUserId));
return TRUE;
} else {
return false;
}


} else {
$this->faildLogins->addFaildLoginToIp($ip);
return FALSE;
}

//md5( md5($sPassword) . md5($sSalt) )
}

$this->faildLogins->addFaildLoginToIp($ip);
return FALSE;
}

最佳答案

将 session 变量设置为用户特有的内容,例如 userid。

$_SESSION['UserId'] = $id;

然后,检查登录函数顶部的 session 变量。

if (isset($_SESSION['UserId']) // user already logged in
$ret = 'ACTIVE';

然后在函数的底部

return $ret;

顺便说一句:我将摆脱函数中的多次返回并使用 $ret 变量,如我的示例中所示。另外,不要忘记在用户注销时删除 session 变量:

unset($_SESSION['UserId']);

此外,您还可以检查任何需要登录用户的页面顶部的 session 变量,如果未设置,则重定向到登录页面。

关于php - Codeigniter单站点登录,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/7787556/

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