I see you are using expLogin as expire time for the cookie. If it is a constant, you should verify its value, the amount of seconds. If expLogin (maybe a typo for $expLogin?) is not defined, php will think it is a string and when casting into an integer, its value is 0.
Trying to set cookies with PHP but aren't setting
-
19-07-2023 - |
سؤال
Alright so I've got a php class called authentication
which is setting some cookies in the login
method if the person is successfully logged in, then my script in the same method login
I am forwarding the user using php's header
to the appropriate url. However for some reason the cookies aren't being set.
Here is the class authentication
:
<?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 );
}
}
?>
Now I've used this method many times however the only difference was that before I was programming the code procedurally and now I've put the same code into a class. After being put into the class using the same syntax for the most part the cookies are not setting properly. Any help would be greatly appreciated!
Thanks
المحلول
لا تنتمي إلى StackOverflow