PHP if $_GET isset and not equal to - php

I have this condition, what I am trying to do is say, if $_GET['subpage'] is not equal to registerupcoming then do this, but I get an error:
Notice: Undefined index: subpage in /Applications/XAMPP/xamppfiles/htdocs/losanihomes/views/includes/header.php on line 26
So I tried the following:
if(isset($_GET['subpage']) && $_GET['subpage'] != 'registerupcoming'){
//Do Something
}
but the problem is, if subpage is not set, then it does not go into the condition, I only want to not go into this condition if subpage is not equal to registerupcoming

Just change your && to || and isset() to !isset():
if(!isset($_GET['subpage']) || $_GET['subpage'] != 'registerupcoming'){
//Do Something
}
Now if that variable isn't set or has the wrong value you will redirect.

Related

Notice: Undefined index with used isset and $_GET

this is my code
if(isset($_GET['delt']) || isset($_GET['editt'])){
$delt = intval($_GET['delt']);
$editt = intval($_GET['editt']);
}
when run the code I get the error
Notice: Undefined index: delt in
/opt/lampp/htdocs/tel-s/admin/top_a.php on line 116
I'm confused because i use isset and test used empty but do get the same problem
Your are testing only if one of params is set and later use both of them no matter if it's actually set.
Use && (and) instead of || (or):
if (isset($_GET['delta']) && isset($_GET['editt']))
OR
if (isset($_GET['delta'], $_GET['editt']))
Check both of variables for exists or change code
if( isset($_GET['delt']) ) {
$delt = intval($_GET['delt']);
}
if ( isset($_GET['editt']) ) {
$editt= intval($_GET['editt']);
}
It must have gotten $_GET['editt'] but couldn't get $_GET['delt'] and threw an error because it couldn't set anything to $delt. Consider #Justinas' answer.
did your url contains both "delt" and "editt" parameters ?
what is the out put of print_r($_GET) ?
isset function checks whether a variable is set or not ?
if you pass a string to intval() it will return 0

How to check if two PHP sessions is set or not

I need to check if this $_SESSION['1'] || $_SESSION['2'] isset or not
I tried this
ob_start();
session_start();
if (!isset($_SESSION['log']) && $_SESSION['log'] !== 1) {
if (!isset($_SESSION['insAdmin']) && $_SESSION['insAdmin'] !== 1) {
header("Location:sessionManager/login.php");
}
}
but if $_SESSION['insAdmin'] isset
I get a notes
( ! ) Notice: Undefined index: log in
C:\wamp\www\mysite\en\admin\headers.php on line 13
You're contradicting yourself:
!isset($_SESSION['log']) & $_SESSION['log'] !== 1
You're basically saying if $_SESSION['log'] is not set and $_SESSION['log'] is not 1 which doesn't make sense.
If $_SESSION['log'] is not set then trying to access it's value through $_SESSION['log'] (with the 2nd expression after &&) will result in an undefined index of course.
You should remove the !:
if (isset($_SESSION['log']) && $_SESSION['log'] !== 1) {
if (isset($_SESSION['insAdmin']) && $_SESSION['insAdmin'] !== 1) {
header("Location:sessionManager/login.php");
}
}
You normally use isset to test if a variable contains a value before checking it's actual value.
But you check if the variable is NOT set !isset($_SESSION['log'] and then check it's value which of course will always cause the notice if log is not set.
You're checking if it's not set, check if it's set instead.
if (isset($_SESSION['insAdmin']) && $_SESSION['insAdmin'] !== 1) {
header("Location:sessionManager/login.php");
}

How to solve "Notice: Undefined index: ..."

how can i hide the php notice by solving the issue.
my code
if(empty($_GET['mode']) && !$_GET['page']) {
include($basepath.'/templates/template.home.php');
}
Notice: Undefined index: page in /var/www/public_html/index.php on line 1
i tried like this
if(empty($_GET['mode']) && !isset($_GET['page']) && !$_GET['page']) {
include($basepath.'/templates/template.home.php');
}
but still showing the
Notice: Undefined index: page in /var/www/public_html/index.php on line 1
how can i solve/fix it ?
The problem is that you use in both statements !$_GET['page'] when using && operator.
When you use !$_GET['page'] in both cases even if you added !isset($_GET['page'] this condition is checked in $_GET['mode'] is empty.
You simple should probably change your statement from:
if(empty($_GET['mode']) && !$_GET['page']) {
include($basepath.'/templates/template.home.php');
}
into
if(empty($_GET['mode']) && !isset($_GET['page'])) {
include($basepath.'/templates/template.home.php');
}
In this case simple if $_GET['page'] is not set (and of course mode is empty) you should include homepage template
Either mode or page isn't present in the array $_GET - so you need to test for it using array_key_exists

Undefined index while checking for COOKIE in PHP

I set a cookie on a visitor's PC to show a notice once every 24 hours. Recently I started getting the following error in my error log:
PHP: undefined index notice in **
I use the following code to check if the cookie exists with value and if not show notice:
if($_COOKIE['notice'] != 'yes')
echo 'notice';
}
I can use the following to avoid the PHP notice:
if((isset($_COOKIE['notice']) || !isset($_COOKIE['notice'])) && $_COOKIE['notice'] != 'yes')
Is there a way to do this check in one step only like I was doing or in a similar way?
Thanks
The index 'notice' in your source does not match the reported index 'test' in the error message. To avoid notices, you validate a variable or an index before reading it.
Here are two functions for the validation.
isset()
Is a positive check that the variable/index is set and not NULL
if (isset($_COOKIE['notice']) && $_COOKIE['notice'] === 'yes') {
// success
echo 'Cookie equals "yes".';
}
You can use && (and) for a second condition, that has to be TRUE, too.
empty()
Is a negative check that the index/variable does not exists or has a value that is considered empty (NULL, 0, '', array()).
if (empty($_COOKIE['notice']) || $_COOKIE['notice'] !== 'yes') {
// error
echo 'Cookie does not exists, is empty or does not equal "yes".';
}
You can use || (or) to combine that with additional conditions that need to be FALSE, too.
Or use the operator:
??
The operator assign a default value if the variable/index is not set or NULL:
$noticeCookie = (string)($_COOKIE['notice'] ?? 'no');
if ($noticeCookie === 'yes') {
// success
echo 'Cookie equals "yes".';
}
You always need to know if a variable is set before try to find what is inside, so yes.. you need to do the check. Also, your logic is wrong.. you are checking if notice is or isn't set (allways true) and checking if notice is "yes".
The code should be something like this:
if ( (!isset($_COOKIE['notice'])) || ( (isset($_COOKIE['notice'])) && ($_COOKIE['notice'] != 'yes') ) ) {
//Show the notice
}

Undefined index in deleting $_SESSION

I have this code to delete any idea from a cart... I need to delete two$_SESSION.
1) $_SESSION["cart_array"]
2 $_SESSION["minicart"]
Without me adding $_SESSION["minicart"] it does delete the $_SESSION["cart_array"] but when i added it i got the minicart part i got an undefined index: minicart. So I
tried
if (!isset($_SESSION["cart_array"]) || count($_SESSION["cart_array"]) && !isset($_SESSION["minicart"]) || count($_SESSION["minicart"]) < 1) {
the code above checks // If the cart session variable is not set or cart array is empty*
to the orginal if (!isset($_SESSION["cart_array"]) || count($_SESSION["cart_array"]) < 1) {
So
<?php
// if user wants to remove an item from cart
if (isset($_POST['index_to_remove']) && $_POST['index_to_remove'] != "") {
// Access the array and run code to remove that array index
$key_to_remove = $_POST['index_to_remove'];
if (count($_SESSION["cart_array"]["minicart"]) <= 1) {
unset($_SESSION["cart_array"]["minicart"]);
} else {
unset($_SESSION["cart_array"]["minicart"] ["$key_to_remove"]);
sort($_SESSION["cart_array"]["minicart"]);
}
}
?>
My quesion
Looking at the tried what I am i doing wrong in the if statement and also what am I doing wrong in the statement to delete the ($_SESSION["cart_array"]) AND ($_SESSION["minicart"])
If this is still unclear please leave a comment and I will do my best to explain it again.
Try changing
if (isset($_POST['index_to_remove']) && $_POST['index_to_remove'] != "") {
to
if (isset($_POST['index_to_remove']) && ($_POST['index_to_remove'])) {
or !empty instead of isset
if (!isset($_SESSION["cart_array"]) || count($_SESSION["cart_array"])
&& !isset($_SESSION["minicart"]) || count($_SESSION["minicart"]) < 1) {
You have a boolean logic problem here. This is not preventing $_SESSION['minicart'], or $_SESSION['cart_array'] for that matter, from being accessed if it is not set. That accessing when not set is what is giving you your error. Check your negations (!), and whether you really want || there.
I don't understand what all you need to check before unsetting those $_SESSION values, I don't have enough information and context. As such, I can't give you code to copy in paste into yours. All I can do is tell you why you're getting undefined index errors. Your if statement, which is supposed to keep $_SESSION['cart_array'] and $_SESSION['minicart'] from being accessed if they aren't set, is not doing what you intended. Trace through it and check your logic. If you can't make it work, then simplify it by breaking that into multiple, nested if statements.
If you want copy and pastable code, you will need to clarify what your doing a bit more. What are you checking with the count() parts, under what circumstances do you want to unset variables, and which variables do you want to unset.

Categories