PHP if SESSION exists but is null - php

How I can check in PHP if SESSION exists but is null.
I have really old self made platform, where visitor can log in as "guest", it sets $_SESSION "guest" but it is NULL.
Now I wan't to show some specific content to only registered. If I check
isset($_SESSION['guest']), is_null($_SESSION['guest']) or is_null($_SESSION['guest'] === FALSE they all return same.
How I can check if guest session exists but is null?

Try this:
if(isset($_SESSION['guest']) && is_null($_SESSION['guest']))
{
// your code
}

if($_SESSION['guest'] == "")
{
// code
}

You can try using empty instead, for example:
session_start();
if(isset($_SESSION['guest']) && !empty($_SESSION['guest'])) {
//Session is not null or empty
}
else
{
//Session is null or empty
}

Related

After switching from file to session vars the functions don't work

I have two methods which work without problems
public static function appendGamer($gamer) {
file_put_contents(self::$fileName, $gamer, FILE_APPEND);
}
and
private function selectLastGamer() {
$gamers = file(self::$fileName);
if (sizeof($gamers) > self::$totalGamers) {
self::$totalGamers = sizeof($gamers);
return trim($gamers[self::$totalGamers - 1]);
}
return "";
}
Now I want everything to be stored in session vars rather than traditional text files. So i rewrite these functions the following way
public static function appendGamer($gamer) {
$old = trim($_SESSION["gamers"]);
$_SESSION["gamers"] = ($old == "") ? $gamer : $old . PHP_EOL . $gamer;
}
and
private function selectLastGamer() {
$gamers = explode(PHP_EOL, $_SESSION["gamers"]);
if (sizeof($gamers) > self::$totalGamers) {
self::$totalGamers = sizeof($gamers);
return trim($gamers[self::$totalGamers - 1]);
}
return "";
}
But now these functions don't work. I mean now selectLastGamer always returns empty string. The session variable is automatically created in constructor so it is always set. What's wrong with new version of these functions?
You should start session on every page like
if( session_status() != PHP_SESSION_DISABLED )
{
session_start();
}
OR
if( session_status() == PHP_SESSION_NONE || session_status() == PHP_SESSION_ACTIVE)
{
session_start();
}
Because session_status() returns any one of the following value at a time.
PHP_SESSION_DISABLED if sessions are disabled.
PHP_SESSION_NONE if sessions are enabled, but none exists.
PHP_SESSION_ACTIVE if sessions are enabled, and one exists.
Hope it ll work for you.
Start session on every page you want to access the session variable set/get using session_start(); at the top of the page.
On main page and also on the page where functions are defined.

Checking POST Value Codeigniter

This is what I am using to check if an post value is empty or not in codeigniter.
if($this->input->post('inputEmail')) { }
Just wanted to know what is the best method to check it, the above mentioned method or
$temp = $this->input->post('inputEmail');
if(!empty($temp)) { }
Check with isset in php to check whether variable is set or not.
Try this:
if(isset($tmp))
{
//it is set
}
else
{
//not set
}

Can not log out correctly PHP

I can log in and access all member pages, but when I log out, I can still access all memberspages
I use this code to log out:
$_SESSION["admin_id"] = false;
$_SESSION["username"] = null;
redirect_to("login.php");
and this code to check if a user is logged in,
function logged_in() {
return isset($_SESSION["admin_id"]);
}
function confirm_logged_in($page) {
if (!logged_in()) {
redirect_to($page);
}
}
he redirects me after i have used the log out code. But i can still type in the member page URL and access them like I am logged in. I use an other webbrowser its impossible, so the pages are protected correctly. Or do I need to destroy the cookie and session complectly?
Here's what you're doing when you logout.
You are setting admin_id to false (which is technically a value).
$_SESSION["admin_id"] = false;
You then check to see whether admin_id is set:
isset($_SESSION['admin_id'); // returns TRUE because it "IS SET" to false
Options
You can either check whether admin_id is not empty (which handles null, false, 0).
function logged_in() {
return ! empty($_SESSION["admin_id"]);
}
You can extend your current function.
function logged_in() {
return isset($_SESSION["admin_id"] && $_SESSION["admin_id"] !== false);
}
You can set the variable to null.
$_SESSION['admin_id'] = null;
You can destroy the session completely.
session_destroy();
In place of:
$_SESSION["admin_id"] = false;
Try this:
unset($_SESSION["admin_id"]);
If you really need to log out, in my opinion your best option is to invalidate the session with a simple session_destroy().
By doing this you can check $_SESSION with the isset() function without problems, since every parameter belonging to the old session has been unset.

What is the proper way to test CodeIgniter session variable?

Take the following code snippet. What is the best way to test to make sure the session variable isn't empty?
<?php if ($this->session->userdata('userID')) {
$loggedIn = 1;
}
else {
$loggedIn = 0;
} ?>
If later in my script, I call the following, the first prints properly, but on the second I receive Message: Undefined variable: loggedIn
<?php echo $this->session->userdata('userID'));
echo $loggedIn; ?>
I've tried using !empty and isset, but both have been unsuccessful. I also tried doing the if/then statement backwards using if (!($this->session->userdata('userID')), but no dice. Any thoughts?
Try doing the following instead:
<?php
$loggedIn = 0;
if ($this->session->userdata('userID') !== FALSE) {
$loggedIn = 1;
}
?>
If the error continues, you'll need to post more code in case you're calling that variable in another scope.
If your aim is to see whether or not the session variable 'userID' is set, then the following should work:
$this->session->userdata('userID') !== false
Why don't you create a boolean field in your session called is_logged_in and then check like:
if(false !== $this->session->userdata('is_logged_in'))
if($this->session->userdata('is_logged_in')) {
//then condition
}
This is the proper way to test!

PHP: testing session

Why is the construction brittle? I tried "!empty ( get_original_passhash() )" as a condition, but it ignites the error that you cannot use the return value.
if ( get_original_passhash () != '' )
{
set_login_session ( get_original_passhash() );
}else
print("Please, log in.");
I would be inclined to assign the variable before you test it, and probably also clean up your formatting a little too:
$original_hash = get_original_passhash();
if ($original_hash != ""){
set_login_session(get_original_passhash());
} else {
print("Please Log In");
}
You should also ensure that get_original_passhash() is returning the right type of variable - interger, string, boolean, etc.
Edit:
function get_original_passhash(){
$dbconn = pg_connect("host=localhost port=5432 dbname=heoa user=heoa password=123");
if(!empty($passhash_session)){
return $passhash_session;
} else {
return $passhash_post;
}
}
What is this code supposed to do? It connects to a database, and then tests a variable that just appears out of nowhere? Your code isn't working because, from the example's you've provided us, nothing is even being set. Is this the full source code for this function?
You may want to split up your logic:
if (is_logged_in()) {
set_login_session(get_original_passhash());
} else {
print("Please Log In");
}
Since, in the conditional, you don't want the pass hash. You want to know if they're logged in or not.

Categories