So I am trying to hide or show options if the user is logged in or not.
I have this simple statement, its always showing no regardless if I am logged in or not.
if( !isset($_SESSION) ){
echo "yes";
}
else {
echo "no";
}
I have also tried
function __construct()
{
parent::__construct();
$this->is_logged_in();
}
if(is_logged_in())
{
echo "yes";
}
else
{
echo "no";
}
Neither works, I also think the first one if simpler, but I am not sure what method would be better.
isset($_SESSION) checks if the variable is set or not and here $_SESSION is already defined.
so in your case !(isset($_SESSION)) is false coz isset($_SESSION) is true and !true is false
To check for the session value try isset($_SESSION['key_you_set']). This will check if the key_you_set exists or not.
Assuming you have set an session with name session_id
You can retrieve your session information in codeigniter like,
$session_id = $this->session->userdata('session_id');
Now You can check like below
if($session_id==""){
echo "session not set";
}
else{
echo "session set";
}
I think this helps you ..
$session_id = $this->session->userdata('session_id');
less code but more secure
echo (!empty($session_id ) && isset($session_id) ) ? "session set" : "session not set" ; // ternary operator
Related
check wether cookies are available or not
$d=0;
//**data is stored in cookies as arrays**
if(is_array($_COOKIE['data']) {
//**data increment by 1 if found**
$d=$d+1;
}
//**if data not found echo data not found**
if($d==0) {
echo "data is not present";
}
else{
echo "data presrnt";
}
I am getting notice undefined variable data
use isset to check vars set or not:
$d=0;
//**data is stored in cookies as arrays**
if(isset($_COOKIE['data']))
if(is_array($_COOKIE['data']){
//**data increment by 1 if found**
$d=$d+1;
}
//**if data not found echo data not found**
if($d==0){
echo "data is not present";
}
else{
echo "data presrnt";
}
you can check the existance of a variable with isset() function like this :
if(isset($_COOKIE['data']) && is_array($_COOKIE['data']){
echo "data present";
} else{
echo "data is not present";
}
you can also check if a variable exist and not empty with empty() function like this :
if(!empty($_COOKIE['data']) && is_array($_COOKIE['data']){
echo "data present and it's not empty";
} else{
echo "data is not present";
}
empty values are : null, "", 0, "0", 0.0, false, [], $var// undeclared var
also i see that your storing an array in the cookie, i suggest for best practice serializing the array before storing in a cookie like this :
setcookie('data', serialize($data), time()+3600);
to get it's value all you have to do is :
$data = !empty($_COOKIE['data']) ? unserialize($_COOKIE['data']) : null;
I am using $_SESSION to store cookies as I need certain variables to be set upon login.php to be used on other place.
On my login.php
if(!isset($_SESSION)) {
session_start();
}
$_SESSION['isLoggedIn'] = 1;
$_SESSION['loggedInID'] = $id;
$_SESSION['isAdmin'] = $isAdmin; // 1 for admin, else 0
On my admin.html page which will call admin.js that would do ajax call to admin.php upon load. I need to check whether is the user logged in is an admin.
I did this on my admin.php.
session_start();
if (!(isset($_SESSION['isAdmin']) && $_SESSION['isAdmin'] != 1)) {
echo 'Not logged in as admin';
}
But now on my admin page, admin.php keeps echo "Not logged in as admin";
Why is that so?
You need OR instead of AND:
if (!isset($_SESSION['isAdmin']) || $_SESSION['isAdmin'] != 1) {
Now, if the variable is set, the first condition is false so the second one will be checked. Only if the second condition is met (not logged in as admin), you will see the warning.
Well, lets say isset($_SESSION['isAdmin'] == true and lets say $_SESSION['isAdmin'] == 1. So currently your if looks like this:
if(!(true && 1!=1)) {
...
}
so you then get
if(!(true && false)) {
...
}
furthermore, you get
if(!(false)) {
...
}
which really means true. Try something like this instead:
session_start();
if (!isset($_SESSION['isAdmin']) || $_SESSION['isAdmin'] != 1) {
echo 'Not logged in as admin';
}
incase of $isAdmin = 1 in the example you give renders to:
if (!(true && false)) {
echo 'Not logged in as admin';
}
this is why you will get the echo result Not logged in as admin
Check if the variable is defined before see it's value
session_start();
if (isset($_SESSION['isAdmin']) {
if($_SESSION['isAdmin'] != 1) {
echo 'Not logged in as admin';
}
}
I am struggling to redirect the user if the cookie does not equal a vairable. If it does equal the vairable, then it should continue the script. Here is my code to redirect :
if(empty($_GET)) {
//No variables are specified in the URL.
//Do stuff accordingly
echo "No variables specified in URL...";
} else {
//Variables are present. Do stuff:
$id = htmlspecialchars($_GET["id"]);
echo 'url query is ' . $id;
}
if(isset($_COOKIE['logged_in']) == $id)
{
header("Location: test.php");
}
if(isset($_COOKIE['logged_in']) != $id)
{
//continues the script
Please note that the vairable in the if statment ($id) is a vairable from the query of url; for example if the url is, "random.com/test.php?id=17" and the cookie equals 18 the script should redirect. However if url is, "random.com/test.php?id=17" and the cookie equals 17, then stay on the same page. Sorry if it sounds complecated.
It doesnt work as this code: It doesnt redirect no matter what the vairable equals. Thanks
Are you looking for something like this. If so, it should work for your case:
<?php
if(empty($_GET)) {
//No variables are specified in the URL.
//Do stuff accordingly
echo "No variables specified in URL...";
} else {
//Variables are present. Do stuff:
$id = htmlspecialchars($_GET["id"]);
echo 'url query is ' . $id;
}
if(isset($_COOKIE['logged_in']) && $_COOKIE['logged_in']==$id)
{
header("Location: test.php");
}
if(isset($_COOKIE['logged_in']) && $_COOKIE['logged_in']!=$id)
{
//continues the script
}
?>
A headers will apply only after it send to client. If you want immediately redirect, you can put exit(0) after header(...) in this case you are stop executing of the script and will send current headers to the browser which will redirect you.
if(isset($_COOKIE['logged_in']) && $_COOKIE['logged_in']==$id) {
header("Location: test.php");
exit(0);
}
//continues the script
The problem is that you are comparing the "value" of isset (the result) with the value of your GET parameter, $id:
if(isset($_COOKIE['logged_in']) == $id)
What this says is "determine if $_COOKIE['logged_in'] is set and compare that determination to $id". PHP will evaluate isset, which returns true or false (as it says in the documentation), and compare that true or false to the other side of the expression (==), meaning $id, which will never match given your examples. If you query "random.com/test.php?id=true" (or false) that might do what you are looking for.
The line you have does not mean "determine if $_COOKIE['logged_in'] is set and compare the value of $_COOKIE['logged_in'] to the value of $id", which I believe is what you are looking for. In that case, what you want to do is first check that $_COOKIE['logged_in'] is set and then check that the value of $_COOKIE['logged_in'] matches $id, like so:
if (isset($_COOKIE['logged_in']) && $_COOKIE['logged_in'] == $id)
If that doesn't make sense, here is a really explicit version that might be clearer as to what is actually going on:
if ((isset($_COOKIE['logged_in']) == true) && ($_COOKIE['logged_in'] == $id))
Hope that helps.
you should add another condition.
if(empty($_GET)) {
//No variables are specified in the URL.
//Do stuff accordingly
echo "No variables specified in URL...";
} else {
//Variables are present. Do stuff:
$id = htmlspecialchars($_GET["id"]);
echo 'url query is ' . $id;
}
if(isset($_COOKIE['logged_in']) && $_COOKIE['logged_in'] == $id)
{
header("Location: test.php");
}
if(isset($_COOKIE['logged_in']) && $_COOKIE['logged_in'] != $id)
{
//continues the script
or use this script
if(isset($_COOKIE['logged_in']))
{
if($_COOKIE['logged_in']==$id){
header("Location: test.php");
}
else{
//another condition to equal is not equal so directly we can use else
//continues the script
}
} else {
echo "Cookie not valid or available";
// redirect user
}
ok forgive my technique in writing here, but i can't seem to understand why this code recognizes things and then doesn't recognize some other things.
my code:
if (!isset($id)) {
$_SESSION["logged_in"] = False;
printf("Could not retrieve records: %s\n", mysqli_error($mysqli));
if ($_SESSION["logged_in"] = False)
{ echo "session variable is set at False"; }
if ( $_SESSION["logged_in"] = True)
{ echo "session variable is set at True"; }
}
now as I see it, if it enters this bit of code at all, the first thing that happens should be that the variable gets set to "false". elsewhere in some code not shown it gets set to true and that part works fine but when i try to force it into this for a false setting it remains true.. can anyone see why this wouldn't get set to "False" at this point in the execution?
You should be using == for comparison here, not = for assignment:
if ($_SESSION["logged_in"] = False)
// ---------------^^^
// Should be
if ($_SESSION["logged_in"] == False)
// Also here:
if ( $_SESSION["logged_in"] = True)
//--------------------------^^
// Should be
if ($_SESSION["logged_in"] == True)
You are making a classic mistake by assigning the variable in your if statement instead of comparing it. So, change your if statements to:
if ($_SESSION["logged_in"] == false)
Instead of (where you are assigning):
if ($_SESSION["logged_in"] = false)
By the way, you're statement has now some duplication since the variable itself already is true of false. So, there is no need to check it against the boolean. So, this can be enough:
if ($_SESSION["logged_in"]) //equals true if user is logged in
if (!$_SESSION["logged_in"]) // equals true if user is NOT logged in
You need == or ===
Using = means its equal :)
if (!isset($id)) {
$_SESSION["logged_in"] = False;
printf("Could not retrieve records: %s\n", mysqli_error($mysqli));
if ($_SESSION["logged_in"] == False)
{ echo "session variable is set at False"; }
if ( $_SESSION["logged_in"] == True)
{ echo "session variable is set at True"; }
}
You should be using the == operator rather than = in your if statements.
EG
if ($_SESSION["logged_in"] == False)
i'm trying to make an image appearance and disappearance based on 3 condition,
condition A = when user is logged in and it's username fits the displayname(by using the GET function) then it should echo "yes"
condition B = When user is logged in and it's username does not fits the displayname then it should echo "no"
condition C = when user is not logged in then it should echo "no" too
(i swapped the image with yes and no for easier referencing)
By logging in, the user has a cookie which is set like below
setcookie("user", $user, $expire);
setcookie("loggedin", 1, $expire);
First i get the cookie which i set when user logins.
$user1 = $_COOKIE["user"];
$loggedin = $_COOKIE['loggedin'];
$user = strtoupper($user1);
then i get my player's name
$playername = $_GET['player'];
Now i do the conditions
$uplayername = strtoupper($playername);
function showplusicon(){
global $uplayername;
if(($loggedin = "1") and ($user == $uplayername)){
echo "yes";
}
else if (($loggedin = "1") and ($user != $uplayername)){
echo "no";
}
else{
echo "no";
}
}
I don't see what's the problem but it keeps being registered as condition B.
Single equal signs assign, not compare.
if(($loggedin == "1") and ($user == $uplayername)){
...
And since you really only have two output states, you shouldn't need 3 conditions; remove condition B.
The variable $loggedin isn't known inside your function showplusicon(). You will need to add it as a global along with global $uplayername.
function showplusicon(){
global $loggedin, $uplayername;
// etc
}
Since this was accepted but not totally complete, I'll just add that as others indicated, the == equality operator needs to be used instead of the = assignment operator.
if(($loggedin == "1")
^^^^
$loggedin = "1"
Surely this should be:
$loggedin == "1"
Otherwise I would echo $user and $uplayername to see if these differ.
First thing's first:
$loggedin = "1" is a bad idea, as you're actually giving $loggedin the value "1" instead of comparing. Use == or even === if you're sure about the datatype.
Further on, the $loggedin isn't available in the scope of showplusicon(), as you haven't declared it as a global like you did with $uplayername.
Fix the listed issues above and it should be working a bit better.
If you've got problems to understand your own code's logic, a simple way is to assign the conditions to self speaking variables to get used to it:
$userIsLoggedIn = $loggedin == "1";
$userIsPlayer = $user == $uplayername;
The variables make it easy to debug your code at the very beginning
var_dump($userIsLoggedIn, $userIsPlayer);
so to locate the actual errors:
The variable $loggedin is undefined
The if clauses are setting a value (=), not comparing it (== or ===).
You can then use additionally a more readable code-flow to make your decision more visible:
if ($userIsLoggedIn)
{ // user is logged in
if ($userIsPlayer)
{ // user is player
...
}
else
{ // user is not player
...
}
}
else
{ // user is not logged in
...
}
Depending of what you want to output, this can be simplified even:
if ($userIsLoggedIn && $userIsPlayer)
{
echo 'yes';
} else
{
echo 'no';
}
Hope this is helpful for you.
Your main problem is todo with global scope of your variables:
<?php
//Get cookie info
$cookie['user'] = $_COOKIE["user"];
$cookie['loggedin'] = (isset($_COOKIE['loggedin'])&&$_COOKIE['loggedin']=='1')?TRUE:FALSE;
//Set user array
$user['user'] = strtoupper($cookie['user']);
$user['loggedin'] = $cookie['loggedin'];
$user['player'] = $_GET['player'];
$user['uplayername']=strtoupper($user['player']);
function showplusicon(){
//Made $user array available within function
global $user;
if($user['loggedin'] === TRUE && $user['user'] == $user['uplayername']){
echo "yes";
}else{
echo "no";
}
}
?>