I am working on this CodeIgniter project, and I want to provide the 'Remember Me' feature for logging in. and for that I am setting a cookie like this.
if(strtolower($loggedin) == 'on') {
//set cookie for 30 days.
setcookie('teacher_login', $teacher_id, time()+60*60*24*30);
}
redirect();
I have checked by using,
echo setcookie('teacher_login', $teacher_id, time()+60*60*24*30);
and the output was '1'. But after the redirect to the home controller, there when I check use the following:
if (isset($_COOKIE["teacher_login"])) {
echo $_COOKIE["teacher_login"];
} else {
echo 'cookie not set';
}
and the output here is 'cookie not set'.
I don't know what is going on here, I checked the PHP manual and the instructions seemed simple enough. Moreover I tried to use the CodeIgniter cookie helper. set_cookie($cookie) didn't work either.
What could possibly be the problem?
Related
I tried to read a PHP cookie but I couldn't, although I was able to see it in the Chrome console. I have found the solution myself, using debugging methods, but in case someone else is having the same problem, I'll answer this question myself, in order to save you time.
I was using this code:
if(!empty($_COOKIE['my.cookie'])) {
echo "cookie value: ".$_COOKIE['my.cookie'];
}
// fallback
else {
echo "cookie not set!";
}
Using the var_dump() function helped me identify the problem, because the cookie-name was changed by PHP, automatically. (Maybe a bug, or some kind of known limit - my version is PHP 7.0.33.)
var_dump($_COOKIE);
This resulted in the list of cookies printed on the web page, and the cookie name was changed from "my.page-LANG" to "my_page-LANG".
So I renamed my cookie and I was good:
if(!empty($_COOKIE['my_cookie'])) {
echo "cookie value: ".$_COOKIE['my_cookie'];
}
// fallback
else {
echo "cookie not set!";
}
So I want to be able to tell, in PHP, whether a session is currently active. I thought that this would be easy by either using session_status() or session_id(), but both return inconclusive results.
My PHP looks like this
if (session_id() == "") {
echo json_encode("nope");
} else {
echo json_encode("yep");
}
if (session_status() === PHP_SESSION_DISABLED) {
echo json_encode("disabled");
} elseif (session_status() === PHP_SESSION_NONE) {
echo json_encode("none");
} else {
echo json_encode("active");
}
$handler = new DatabaseSessionHandler();
session_set_save_handler($handler, true);
session_start();
And I have an Angular app making http calls to this script to basically activate its session.
I've been testing this, and when I visit the site for the first time, no session_id is found and the session_status() returns back "none", as they both should. But the problem is, when I refresh the page, and this PHP script is runs, but this time I have an active session (the PHP session cookie shows up in my browser), session_id is still acting like none exists. And also, session_status() returns back, mistakenly "none".
Any ideas would be greatly appreciated, thanks!
Edit 1: A few people have mentioned that putting the session_start() in front of testing if it is active or not should work. When I do this, a session_id is always found, and the session_status() always returns back "active". Even when a fresh new user visits the site, this still happens.
The session won't be active until you call session_start(), which you don't do until after testing to see if the session is active.
You should call session_start() at Top of the page and then perform the condition on it. In your case you are not starting session and you are testing for it.
I am not sure if this will solve your problem, but you could check if your session directory is writable:
if (!is_writable(session_save_path())) {
echo 'Session path "'.session_save_path().'" is not writable for PHP!';
}
A question related to that is at PHP Session not Saving.
Elsewhere try the following:
if (session_id() == "") {
session_start();
echo json_encode("nope");
} else {
echo json_encode("yep");
}
So, I'm using a simple login-script for a few pages, using sessions to identify the user, by putting the users name in a $_SESSION variable and then checking if the variable is set on each page. This worked fine when I ran the script locally through a WAMP-server - but when I uploaded it to a webhotel I ran into a weird issue.
The login-script, which sets the $_SESSION variable if the username and password matches up with information from a MySQL-database, somehow won't start the session. I have session_start(); at the top of every page, including the login-script, so I don't understand why it wont start.
Now, I found a script on here that is used to check for session-support:
<?php
// Start Session
session_start();
// Show banner
echo '<b>Session Support Checker</b><hr />';
// Check if the page has been reloaded
if(!isset($_GET['reload']) OR $_GET['reload'] != 'true') {
// Set the message
$_SESSION['MESSAGE'] = 'Session support enabled!<br />';
// Give user link to check
echo 'Click HERE to check for PHP Session Support.<br />';
}
else {
// Check if the message has been carried on in the reload
if(isset($_SESSION['MESSAGE'])) {
echo $_SESSION['MESSAGE'];
}
else {
echo 'Sorry, it appears session support is not enabled, or you PHP version is to old. Click HERE to go back.<br />';
}
}
?>
The really weird thing is that this script tells me that session-support is enabled - and after running it, it suddenly works across all the pages.
So I have to run this script (in its own file) every time I access the site, because the login-script won't set the $_SESSION variable without running this script first.
JUST TO MAKE SURE: I am NOT asking how to check if session-support is enabled or not; the issue is why sessions are not enabled untill AFTER I run the script above.
Why is this happening, and how do I fix it?
Session is automatically started when session_start() function runs. To check if a session is set, you do not need that long code. Try this.
if(session_start())
{
echo session_id();
}
If session is started, session id will be printed. Else, it won't.
At first, my sessions kept resetting so I wrote out a simple script to test out my sessions.
<?php
session_start();
// Show banner
echo '<b>Session Support Checker</b><hr />';
// Check if the page has been reloaded
if(!isset($_GET['reload']) OR $_GET['reload'] != 'true') {
// Set the message
$_SESSION['MESSAGE'] = 'Session support enabled!<br />';
// Give user link to check
echo 'Click HERE to check for PHP Session Support.<br />';
} else {
// Check if the message has been carried on in the reload
if(isset($_SESSION['MESSAGE'])) {
echo $_SESSION['MESSAGE'];
} else {
echo 'Sorry, it appears session support is not enabled, or you PHP version is to old. Click HERE to go back.<br />';
}
}
?>
Needless to say, I got "Sorry, it appears session support is not enabled..." (my php version is 5.2 if memory serves, so it's definitely not too old). I checked my php.ini file and PHP is writing session files in the folder the php.ini file points to with what appears to be correct data, yet, I can't pass any data from one page to the other. Anybody have any idea on what is going wrong?
Here's the pertinent part (session) of phpinfo()...
http://pastebin.com/vnv7J26T
First off, please bear with the question as I am just starting out with this.
So I have the following running in an index.php:
<?php
if(isset($_COOKIE['language'])) {
if (($_COOKIE['language']) == 'en')
print "<SCRIPT LANGUAGE=\"JavaScript\">window.location=\"URLGOESHERE\"</SCRIPT>";
elseif (($_COOKIE['language']) == 'fr')
print "<SCRIPT LANGUAGE=\"JavaScript\">window.location=\"URLGOESHERE\"</SCRIPT>";
}
else {
print "<SCRIPT LANGUAGE=\"JavaScript\">window.location=\"URL-FOR-COOKIE-SETTING-PAGE\"</SCRIPT>";
}
?>
The "else" statement redirect goes to the page where the user sets their language preference, and once they do so, that page sets a cookie then sends them to the proper version of the site. However, when the user goes back to this main index.php, it cannot seem to find the cookie and skips right to the "else" statement. The redirects used to be used done with PHP using 'header,' and it didn't work then either. I had read elsewhere that that could pose some troubles, so I switched it to try printing Javascript.
The weird thing is that I can find the cookie as soon as it is set, exactly where it should be, with the correct name and variable all present. I've done it exactly like the books tell me to (to my knowledge). I've tried this both in Firefox and Safari with no luck.
What did I miss?
EDIT: Here's the script that actually sets the cookie. The parameter is sent from the link via a url encode like this: <a href="setlang.php?lang=en">
<?php
$lang = urlencode($_GET["lang"]);
setcookie("language", $lang, time()+60*60*24*90, ".URL");
switch ($lang) {
case 'en':
header('Location: URLGOESHERE');
break;
case 'fr':
header('Location: URLGOESHERE');
break;
}
?>
the cookie isnt available to the script that set it.
you have to set the cookie and then on the next page load the cookie will be available to the php code
Try this code:
setcookie("language", $lang, time()+60*60*24*90, "/");
I'm uncertain what .URL was supposed to do o.o