gpt4 book ai didi

php - 使用 PHP 在第 7 天使 Cookie 过期

转载 作者:行者123 更新时间:2023-11-29 11:46:52 29 4
gpt4 key购买 nike

我已在我的 user.php 上设置了 cookie,但它不起作用。

当我关闭浏览器时它正在删除。我想设置登录cookie的有效期为7天。

我添加的代码

setcookie('email', $email,time()+3600*24*7);
setcookie('password', $password,time()+3600*24*7);

我已经尝试了很多次在我的代码上移动和替换它,但它仍然不起作用,我在哪里替换我的代码上的 cookie?

代码:

登录.php

<?php 
ob_start();
session_start();
require_once 'config.php';
?>
<?php
if( !empty( $_POST )){
try {
$user_obj = new Cl_User();
$data = $user_obj->login( $_POST );
if(isset($_SESSION['logged_in']) && $_SESSION['logged_in']){
header('Location: home.php');
}
} catch (Exception $e) {
$error = $e->getMessage();
}
}
//print_r($_SESSION);
if(isset($_SESSION['logged_in']) && $_SESSION['logged_in']){
header('Location: home.php');
}
?>
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="utf-8">
<meta http-equiv="X-UA-Compatible" content="IE=edge">
<meta name="viewport" content="width=device-width, initial-scale=1">
<title>Smart Login Page</title>
<link href='http://fonts.googleapis.com/css?family=Pacifico' rel='stylesheet' type='text/css'>
<!-- Bootstrap -->
<link href="css/bootstrap.min.css" rel="stylesheet">
<link href="css/font-awesome.min.css" rel="stylesheet">
<link href="css/login.css" rel="stylesheet">
<!-- HTML5 Shim and Respond.js IE8 support of HTML5 elements and media queries -->
<!-- WARNING: Respond.js doesn't work if you view the page via file:// -->
<!--[if lt IE 9]>
<script src="https://oss.maxcdn.com/html5shiv/3.7.2/html5shiv.min.js"></script>
<script src="https://oss.maxcdn.com/respond/1.4.2/respond.min.js"></script>
<![endif]-->
<!-- jQuery (necessary for Bootstrap's JavaScript plugins) -->
<script src="js/jquery.min.js"></script>
<!-- Include all compiled plugins (below), or include individual files as needed -->
<script src="js/bootstrap.min.js"></script>
</head>
<body>
<div class="container">
<?php require_once 'templates/ads.php';?>
<div class="login-form">
<?php require_once 'templates/message.php';?>
<h1 class="text-center">Login</h1>
<div class="form-header">
<i class="fa fa-user"></i>
</div>
<form id="login-form" method="post" class="form-signin" role="form" action="<?php echo $_SERVER['PHP_SELF']; ?>">
<input name="email" id="email" type="email" class="form-control" placeholder="Email address" autofocus>
<input name="password" id="password" type="password" class="form-control" placeholder="Password">
<button class="btn btn-block bt-login" type="submit" id="submit_btn" data-loading-text="Signing In....">Sign in</button>
</form>
<div class="form-footer">
<div class="row">
<div class="col-xs-6 col-sm-6 col-md-6">
<i class="fa fa-lock"></i>
<a href="forget_password.php"> Forgot password? </a>

</div>

<div class="col-xs-6 col-sm-6 col-md-6">
<i class="fa fa-check"></i>
<a href="register.php"> Sign Up </a>
</div>
</div>
</div>
</div>
</div>
<!-- /container -->
<script src="js/jquery.validate.min.js"></script>
<script src="js/login.js"></script>
</body>
</html>
<?php ob_end_flush(); ?>

用户.php

    <?php
/**
* This User will have functions that hadles user registeration,
* login and forget password functionality
* @author muni
* @copyright www.smarttutorials.net
*/
class Cl_User
{
/**
* @var will going contain database connection
*/
protected $_con;

/**
* it will initalize DBclass
*/
public function __construct()
{
$db = new Cl_DBclass();
$this->_con = $db->con;
}

/**
* This method will handle user login process
* @param array $data
* @return boolean true or false based on success or failure
*/
public function login( array $data )
{
$_SESSION['logged_in'] = false;
if( !empty( $data ) ){

// Trim all the incoming data:
$trimmed_data = array_map('trim', $data);

// escape variables for security
$email = mysqli_real_escape_string( $this->_con, $trimmed_data['email'] );
$password = mysqli_real_escape_string( $this->_con, $trimmed_data['password'] );


if((!$email) || (!$password) ) {
throw new Exception( LOGIN_FIELDS_MISSING );
}
$password = md5( $password );
$query = "SELECT member_id, member_display_name, member_email, member_status, roles_id FROM fm_member where member_email = '$email' and member_pwd = '$password' ";
//$query = "SELECT user_id, name, email, created, roles_id, id FROM users where email = '$email' and password = '$password'"
$result = mysqli_query($this->_con, $query);
$data = mysqli_fetch_assoc($result);
$count = mysqli_num_rows($result);
mysqli_close($this->_con);
if( $count == 1){
$_SESSION = $data;
setcookie('email', $email,time()+3600*24*7); // This is cookie that i've added.
setcookie('password', $password,time()+3600*24*7); // This is cookie that i've added.
if($_SESSION['member_status'] == 'Activated') {
$_SESSION['logged_in'] = true;
return true;
} else {
throw new Exception( 'Your account is Deactiavted! <br> Please contact to Adminnistrator for more information.' );
$_SESSION['logged_in'] = false;
}
}else{
throw new Exception( LOGIN_FAIL );
}
} else{
throw new Exception( LOGIN_FIELDS_MISSING );
}

}


/**
* This handle sign out process
*/
public function logout()
{
session_unset();
session_destroy();
header('Location: index.php');
}
?>

最佳答案

可能您必须设置 session 的最长生命周期。您可以使用以下命令进行此设置

ini_set('session.cookie_lifetime', 604800);
ini_set('session.gc_maxlifetime', 604800);
session_set_cookie_params(604800, "/");
session_start();

关于php - 使用 PHP 在第 7 天使 Cookie 过期,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/34782313/

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