尝试使用PHP设置Cookie,但没有';t设置
好的,我有一个名为尝试使用PHP设置Cookie,但没有';t设置,php,security,authentication,cookies,setcookie,Php,Security,Authentication,Cookies,Setcookie,好的,我有一个名为authentication的php类,它在login方法中设置了一些cookies,如果用户成功登录,那么我的脚本将使用相同的方法login使用php的头将用户转发到相应的url。但是由于某些原因,cookies没有被设置 下面是类身份验证: <?php require '../../config.php'; require base .'lib/helpers/bao.php'; class authentication extends bao { /*
authentication
的php类,它在login
方法中设置了一些cookies,如果用户成功登录,那么我的脚本将使用相同的方法login
使用php的头将用户转发到相应的url。但是由于某些原因,cookies没有被设置
下面是类身份验证:
<?php
require '../../config.php';
require base .'lib/helpers/bao.php';
class authentication extends bao {
/*
Login
*/
public function login( $email, $pass, $fUrl = null ) {
// set the forwarding url
$forward = '/login.php';
// check for empty values
if( !empty( $email ) && !empty( $pass ) ) {
// set params for binding and run query
$params = array( ':email' => $email );
$runQuery = parent::query( "SELECT * FROM users WHERE email = :email", $params );
// check to see if an account was found
if( parent::numRows( $runQuery ) > 0 ) {
// place result into array for use
$result = parent::fetch( $runQuery );
// check to see if passwords match
if( $result['pword'] === sha1( $result['salt'] . $pass ) ) {
// update login in time in the database
$params = array(':login' => expLogin, ':id' => $result['id'] );
$updateLoggedin = parent::query( "UPDATE users SET login = :login WHERE id = :id", $params );
// Set a bunch of cookies
setcookie("userId", $result['id'], expLogin, '/');
$emailCookie = setcookie("userEmail", $result['email'], expLogin, '/');
setcookie("userNotifications", base64_encode( $result['email'] ), expLogin, '/');
setcookie("userType", $result['type'], expLogin, '/');
setcookie("userStatus", $result['status'], expLogin, '/');
setcookie("userLogin", $result['login'], expLogin, '/');
// set forwarding url
switch( $result['type'] ) {
case 'obl':
$forward = $fUrl ? $fUrl : '/admin/';
break;
case 'broker':
$forward = $fUrl ? $fUrl : '/admin/';
break;
case 'borrower':
$forward = $fUrl ? $fUrl : '/admin/';
break;
}
$processMeta = 'Congrats you are logged in!';
} else {
$processMeta = 'Your passwords did not match, please try again!';
}
} else {
$processMeta = 'I\'m sorry we could not locate your account';
}
} else {
$processMeta = 'You did not enter one of the required fields please try again!';
}
// set process message to session
$_SESSION['processMeta'] = $processMeta;
// forward onto next url
header("Location: ". $forward );
}
}
?>
现在我已经多次使用这种方法,但唯一的区别是,在我按程序编写代码之前,现在我将相同的代码放入一个类中。在大多数情况下使用相同的语法将cookie放入类中后,cookie设置不正确。任何帮助都将不胜感激
谢谢我看到您正在使用expLogin作为cookie的过期时间。
如果它是一个常量,您应该验证它的值,即秒数。
如果没有定义expLogin(可能是$expLogin的拼写错误?),php会认为它是一个字符串
当转换成整数时,它的值是0。fark,你的权利,出于某种原因,我忘了你必须使用unix时间戳,而我使用的是iso8601格式!谢谢