激活后使用PHP身份验证或会话登录

激活后使用PHP身份验证或会话登录,php,Php,我是PHP新手,在激活成功后使用PHP身份验证(auth)或会话是否有任何方法可以直接登录用户 这是我的激活脚本: <?php require_once('recaptchalib.php'); $privatekey = "your_private_key"; $resp = recaptcha_check_answer ($privatekey, $_SERVER["REMOTE_ADDR"], $_POST["recaptcha_challenge_field"], $_POST["

我是PHP新手,在激活成功后使用PHP身份验证(auth)或会话是否有任何方法可以直接登录用户

这是我的激活脚本:

<?php
require_once('recaptchalib.php');
$privatekey = "your_private_key";
$resp = recaptcha_check_answer ($privatekey,
$_SERVER["REMOTE_ADDR"],
$_POST["recaptcha_challenge_field"],
$_POST["recaptcha_response_field"]);

if (!$resp->is_valid) {
// What happens when the CAPTCHA was entered incorrectly
die ("The reCAPTCHA wasn't entered correctly. Go back and try it again." .
"(reCAPTCHA said: " . $resp->error . ")");
} else {
$username = $_POST['username'];
$activation_code = $_POST['activation_code'];
$db_host = "localhost";
$db_name = "databasename";
$db_use = "root";
$db_pass = "password";
$link = mysqli_connect($db_host, $db_use, $db_pass);
mysqli_select_db($db_name, $link);
$command = "UPDATE email_activation SET check_activation='$activation_code' WHERE username='$username' and activation='$activation_code'";
$result = mysqli_query($command);
if ($result) {
echo "Congratulations. Your membership has been activated …";
}else{
echo ("You've entered an invalid username / activation code – please retry");
}
}
?>

是的,你可以。。把这个代码放在后面

if ($result) {
echo "Congratulations. Your membership has been activated …";
$_SESSION['user_logged'] = '1';
header("location:afterloginpage.php");
}else{
echo ("You've entered an invalid username / activation code – please retry");
}
}
或者,如果要保存会话,则可以始终检查会话是否已设置

if (isset($_SESSION['user_logged'])) {
  //User directly logged in
}else {
  //Navigate user to login page
  header("location:loginpage.php");
}

也不要忘记初始化会话_start();在php文件的顶部,您可以执行类似的操作

if ($result) {
    header("Refresh: 5; url=next_file.php");
    echo "Congratulations. Your membership has been activated … If your not redirected in 5 seconds, please click <a href='next_file.php'>here</a>";
}
if($result){
标题(“Refresh:5;url=next_file.php”);
echo“祝贺您。您的会员资格已被激活…如果您在5秒内未重定向,请单击”;
}

您是否正在呼叫
会话\u启动
?如何使用会话启动?