I'm currently working through a large number of warnings I have logged after my server was updated to PHP 8.
One particular point that has me scratching my head is the following code block
if (!$option['type'] || $option['type'] == "select") {
$output .= '<b>'.$option['title'].':</b>';
}
I know that I can use isset($option['type']) like this
isset($option['type']) && $option['type'] == "select"
but confused how that would work for
!$option['type']
How can you be checking if isset, but also it being NOT. If it's NOT, then surely it isn't set anyway?
The test for "may not exist or may be falsey" is:
if (empty($option['type']))
Related
I'm setting $_SESSION['showroom'] to 'active' when a particular page in Wordpress is displayed:
if(get_the_ID()==6470||get_the_ID()==252){
$_SESSION['showroom']='active';
}
I then set 2 arrays of pages to check against. If the next page displayed is NOT in one of these arrays, $_SESSION['showroom'] gets changed to 'inactive'.
$allowed_templates = array('template-A.php',
'template-B.php',
'template-C.php',
'template-E.php',
'template-G.php');
$allowed_ids = array(6470,252);
$template_name = get_page_template_slug();
$page_id = get_the_ID();
if(in_array($template_name,$allowed_templates)==false && in_array($page_id,$allowed_ids)==false){
$_SESSION['showroom']='inactive';
}
The if statement works most of the time, but sometimes my $_SESSION['showroom'] changes to inactive EVEN though one of the arrays is returning true! After several hours of testing I am unable to locate where the problem is. Echoing out the two parts of the if statement ALWAYS gives me 2 trues or 1 true + 1 false, but never 2 falses:
if(in_array($template_name,$allowed_templates)==false){echo 'TFALSE';}
if(in_array($template_name,$allowed_templates)){echo 'TTRUE';}
if(in_array($page_id,$allowed_ids)==false){echo 'IFALSE';}
if(in_array($page_id,$allowed_ids)){echo 'ITRUE';}
What am I missing here?
Thanks in advance for any help!
EDIT: Have continued testing and found the following anomaly:
if(in_array($template_name,$allowed_templates)==false && in_array($page_id,$allowed_ids)==false){
$_SESSION['showroom']='inactive';
echo 'SET TO INACTIVE';
}
The if statement changes $_SESSION['showroom'] to 'inactive' but DOES NOT echo out 'SET TO INACTIVE'!
There's something strange going on here!
Problem solved. My code was fine. Two missing images files were causing WordPress to crash my sessions. Took 10 hours to find out but happy I found it. Thanks to everyone for their help.
You can try the following;
if(!in_array($template_name,$allowed_templates) && !in_array($page_id,$allowed_ids)){
$_SESSION['showroom']='inactive';
}
Edit: lets try and break it down further... similar to your examples
if(!in_array($template_name,$allowed_templates){
echo "not in templates,";
}
if(!in_array($page_id,$allowed_ids)){
echo "not in ids,";
}
if(!in_array($template_name,$allowed_templates) && !in_array($page_id,$allowed_ids)){
echo "not in both\n";
}
then see if we get a result with not in templates,not in ids, but no trailing not in both
The problem is pure logical. Lets look at this statement:
if (in_array($template_name,$allowed_templates)==false && in_array($page_id,$allowed_ids)==false)
Which translates to "If the template is not valid AND page is not valid"
This means that both statements needs to be fulfilled in order to mark session as inactive. What if the template is fine, but the page is not valid? That definitely should be marked as inactive as well.
By changing the statement to read "If the template is not valid OR page is not valid", we cover up the invalid cases. Because either of them counts as an invalid state, and thus, only one of them needs to be false in order for everything to be false. (the OR-statement)
So code-wise it would be
if (in_array($template_name,$allowed_templates)==false || in_array($page_id,$allowed_ids)==false)
And you are set.
As and addition. I would structure the code as you noted works. Which is more logical. That is, mark it as inactive whenever it's should be treated as inactive, in all other cases mark it as 'active'. Or vice-versa.
I'm trying to display items a specific way...
First I want to check if $MYCAR_model exists (this is a session value). If not, do nothing.
Next I want to make sure that the URL variable cat is set to either 1 or 2. If not, also do nothing.
The URL would look like this...
http://www.mysite.com/?cat=1
My failed code...
if (isset($MYCAR_model) && ($cat=='1' || $cat=='2')) {
// show stuff
}
Thank you for any help you can provide.
Maybe
if (isset($_SESSION["MYCAR_model"]) ...
You can also use $_REQUEST["cat"] instead of $_GET["cat"] so that you don't need to worry about the parameter being passed via GET/POST/COOKIES.
if (isset($_SESSION['MYCAR_model']) && ($_GET['cat'] =='1' || $_GET['cat'] =='2')) {
// show stuff
}
hi I am trying to see if i can reverse this if statement so that I can have an if condition which lists results only if the user session is not the user id.
I'm brand new to mysql and php and would really appreciate if someone could show me what i need to do
here's the if statement I'm trying reverse so if $user['id'] is not $_SESSION['user_id']
if ($user['id'] == $_SESSION['user_id']){
I've tried this: (doesn't work and brings up a syntax error)
if (!isset($user['id'] == $_SESSION['user_id'])){
This is fairly simple and actually basic PHP stuff:
if ($user['id'] != $_SESSION['user_id'] )
In addition to Sturb's answer you can also reverse the variables:
if($_SESSION['user_id'] != $user['id'])
Just check using if condition
if(!$user['id']==$_SESSION['user_id'])
I hope this is simple. I am working on a site where a user can register directly with the site or sign in with Facebook etc. What I want to know is how to handle the session data at the top of the script?
Here is what I have:
if ($_SESSION['loggedin'] == 'true') {
}
I have tried:
if ($_SESSION['loggedin'] == 'true')|| if $user{
But this is throwing out an error: unexpected T_BOOLEAN_OR
Is this even the right way of doing this kind of session work? Or should I be approaching this differently? Has anybody else done this that they would be willing to share?
Thanks,
Lewis
The proper syntax is NOT
if ($_SESSION['loggedin'] == 'true')|| if $user{...}
it is...
if ($_SESSION['loggedin'] == 'true' || $condition2==true){...}
All of your conditions in their logical order must be contained in the if's parantheses. Since if statements always check for true, so this can be simplified like so
if ($_SESSION['loggedin'] || $condition2){...}
That's basic PHP syntax:
if (($_SESSION['loggedin'] == 'true') || ($user whatever....)) {
...
}
I have this code to check that my ToS checkbox is checked:
if (!isset($_POST['tosagree'])) {//if the user did not agree to ToS
$errors[] = '<span>Please agree to the Terms of Service.<span>';
}
For some reason it is allowing me to register even with that code there. The weird thing is that if any other field is not field and that field is not checked it will print the error, but if that is the only problem it allows the user to register.
Update: seems like its a problem with something weird in my code. If you feel like looking through 217 lines of code here is all the code: http://pastebin.com/YkERYpeF
Whether !isset($_POST['tosagree']) will be enough depends on the browser.
To be safe, always set the value attribute of the checkbox. Then, you simply check if that value is present:
<input type='checkbox' value='agreed'/>
Then do this:
if (!isset($_POST['tosagree']) || $_POST['tosagree'] != "agreed" ) {
$errors[] = '<span>Please agree to the Terms of Service.<span>';
}
Also, when the value attribute is not explicitly specified, browsers will usually set value to On when it is checked.
Edit
Ok, I know what's happening. In your PHP code, you have this block:
if (!isset($_POST['tosagree']) || $_POST['tosagree'] != "agreed") {//if the user did not agree to ToS
$errors[] = '<span>Please agree to the Terms of Service.<span>';
}
Then later you have this line:
if ($un && $e && $p && $ic) { //if there are no errors
The problem is that you don't have a flag for when the TOS checkbox isn't checked. The error can't be printed because in the if statement you have a call to exit().