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

时间:2014-05-18 16:17:36

标签: 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["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");
}
}
?>

2 个答案:

答案 0 :(得分:1)

是的,你可以..只需将此代码放在此

之后
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");
}

也不要忘记初始化session_start();在php文件的顶部

答案 1 :(得分:0)

你可以这样做。

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>";
}