Execute on certain url format with strpos - php

I want to perform an action when the url is /bookings , but not /bookings/something-else
I tried this...
if ($_SERVER['REQUEST_URI'] == '/bookings') {
// do stuff....
}
But it fails when the user is on the /bookings page and searches, at which point queries are added to the url, e.g. /bookings?search=this
I have also tried this...
if (strpos($_SERVER['REQUEST_URI'],'/bookings') !== false && strpos($_SERVER['REQUEST_URI'],'/bookings/') == false ) {
// do stuff...
}
But this still executes on /bookings/some-thing and i cant figure out why?

You'll be better off using a dedicated method for URL parsing, rather than using string manipulation. PHP's parse_url function is perfect for this:
$path = parse_url($_SERVER['REQUEST_URI'], PHP_URL_PATH);
if ($path === '/bookings') {
...
}

Try this condition. first will make exact string. while seocnd match with query sring
if ($_SERVER['REQUEST_URI'] == '/bookings' || strpos($_SERVER['REQUEST_URI'],'/bookings?') !== false) {
// do stuff....
}
OR use === while matching false/ otherwise 0 and false become equal
if (strpos($_SERVER['REQUEST_URI'],'/bookings') !== false && strpos($_SERVER['REQUEST_URI'],'/bookings/') === false ) {

Related

Condition works separately but not as OR

I have two conditional statements that are the following :
if( isset( $query_string['page'] ) && strpos($_SERVER['REQUEST_URI'], '/blog/') !== false && strpos($_SERVER['REQUEST_URI'], '/blog/page/') === false ) {
if( $query->is_main_query() && !$query->is_feed() && !is_admin() && strpos($_SERVER['REQUEST_URI'], '/blog/') !== false && strpos($_SERVER['REQUEST_URI'], '/blog/page/') === false ) {
The last condition in both if statements is :
strpos($_SERVER['REQUEST_URI'], '/blog/page/') === false
I would like to change the last condition for both, which would be in plain English :
If all criteria are matched and the URL contains either '/blog/page/' or '/blog/tag/' do something.
When I interchange the last condition from '/blog/page/' to '/blog/tag/' the code works. As soon as I try to have both at the same time, the code doesn't work anymore.
I've tried to change the && to and and use || for the or condition, in order to keep the right precedence. I have tried to put them between parenthesis in order to handle the precedence, none of them worked.
I even tried :
strpos($_SERVER['REQUEST_URI'], '/blog/page/') || strpos($_SERVER['REQUEST_URI'], '/blog/tag/') === false
Which didn't help either.
<?php
// Your code says "=== false" (doesn't match)
// but your English description says "contains either '/blog/page/' or '/blog/tag/'" (match)
// This assumes you want what your English description says
/**
* Returns a boolean indicating if the given URI part is found
*/
function match($uriPart)
{
return strpos($_SERVER['REQUEST_URI'], $uriPart) !== false;
}
/**
* Returns a boolean indicating if the given URI part is not found
*/
function doesNotMatch($uriPart)
{
return strpos($_SERVER['REQUEST_URI'], $uriPart) === false;
}
// In this case, "match('/blog/')" is redundant because you're checking for other strings which contain it.
// Nevertheless, I'm leaving it as-is.
if( isset( $query_string['page'] ) && match('/blog/') && (match('/blog/page/') || match('/blog/tag/'))) {
...
// In this case, "match('/blog/')" is redundant because you're checking for other strings which contain it.
// Nevertheless, I'm leaving it as-is.
if( $query->is_main_query() && !$query->is_feed() && !is_admin() && match('/blog/') && (match('/blog/page/') || match('/blog/tag/'))) {
...
}

If statement behaves strange

I have 2 URLs say
http://localhost/xyz?language=en
http://localhost/xyz?language=es
for which I want to check if language parameter has something other than en/es, then it should redirect to some http://localhost/xyz/errorpage
For this I have below code:
if(isset($_GET['language'])){
if(($_GET['language'] !== "en") || ($_GET['language'] !== "es")){
header('Location: /xyz/errorpage');
}
}
But practically when I execute the any of the 2 URLs or putting value of language parameter to something different than en/es:
http://localhost/xyz?language=en
http://localhost/xyz?language=es
http://localhost/xyz?language=esdfsdf
I am redirected to errorpage
Cannot understand the issue with code.
Replace || by &&.
The reason :
You want to redirect only if this is not en AND not es.
change the if statment to && instead of || or your condition will be always false.
if(isset($_GET['language'])){
if($_GET['language'] !== "en" && $_GET['language'] !== "es"){
header('Location: /xyz/errorpage');
}
}
You have bad condition, or better, operator.
Use && instead of ||, or in_array().
if(($_GET['language'] !== "en") && ($_GET['language'] !== "es")) {
Using in_array() function:
if (!in_array($_GET['languge'], array('en', 'es'))) {
header ();
}
Condition if ($a != 'x' || $a != 'y') is always true, first or the second part of condition has be true. There are no other ways.

php: I need the OPPOSITE of this please...?

The following searches the string $fruit for any of the words apples, oranges and bananas and acts accordingly:
if ((stristr($fruit,'apples')) || (stristr($fruit,'oranges')) || (stristr($fruit,'bananas')) !== false) {//some code }
I need the OPPOSITE. I need code to run if the string $fruit does NOT have any of the three. I was thinking something like
if not ((stristr($fruit,'apples')) || (stristr($fruit,'oranges')) || (stristr($fruit,'bananas')) !== false) {//some code }
but that doesnt seem to work...
A little hand holding please? Thanks...
What you're looking for is DeMorgan's law:
The negation of a conjunction is the disjunction of the negations.
The negation of a disjunction is the conjunction of the negations.
Try:
if ( stristr($fruit, 'apples') === FALSE && stristr($fruit,'oranges') === FALSE && stristr($fruit,'bananas') === FALSE) {
// some code
}
Since stristr only returns a string or FALSE, this code can be simplifed to:
if ( !stristr($fruit, 'apples') && !stristr($fruit,'oranges') && !stristr($fruit,'bananas')) {
// some code
}
Thanks for the help everyone.
I ending up doing this, based on all input from those that replied, which worked:
if (!((stristr($fruit,'apples')) || (stristr($fruit,'oranges')) || (stristr($fruit,'bananas')) !== false))
Again, thanks for the help!
if (!(stristr($fruit,'apples')) || !(stristr($fruit,'oranges')) || !(stristr($fruit,'bananas')) !== false) {//some code }

PHP: If-statement, need value of condition that triggered

I'm sorry for the vaguely described title. This is what I want:
if($a[$f] === false || $a[$g] === false || $a[$h] === false || $a[$i] === false || $a[$j] === false)
{
// do something
}
I want to do something with the condition that actually triggered the statement (if a[$f] = true and a[$g] = false, I want to do something with $g).
I know that in this case, the first statement that went true (i.e. $a[$g] == false) triggers. But is there any way to do something with $g? I've never seen this in my programming life before and can't seem to find anything about it.
Thanks in advance.
--- Edit ---
I forgot to mention: I'm using a function on all the array data. So, shortened, I get this:
if(valid($a[$f]) === false || valid($a[$g]) === false)
{
// do something
}
--- Edit 2 ---
This piece of OOP-based PHP, where I'm in a class, is my code.
if($this->validatedText($product[$iName]) == false ||
$this->validatedUrl($product[$iUrl]) == false ||
$this->validatedNumber($product[$iTax]) == false ||
$this->validatedValuta($product[$iPrice]) == false ||
$this->validatedText($product[$iArticleNumber]) == false ||
$this->validatedText($product[$iDescription]) == false ||
$this->validatedText($product[$iMetaDescription]) == false ||
$this->validatedText($product[$iTitle]) == false)
{
// do something with the first iVariable
}
Simplest solution will be
if(false!==($sIndex = array_search(false, $a, 1)))
{
//your $sIndex is first index with false value
}
if you want all keys, you may use array_filter(), like this:
$rgFalse = array_keys(array_filter($a, function($x)
{
//here valid is your function
return false===valid($x);
}));

If $_POST is empty Multiple function

I have the following $_POST function to check if the fields of 'start', 'middle' and 'end' is empty or not.
if(!empty($_POST['start'])) {
$description = "a sentence".$_POST['start']." with something in the START.";
}
if(!empty($_POST['middle'])) {
$description = "a sentence".$_POST['middle']." with something in the MIDDLE.";
}
if(!empty($_POST['end'])) {
$description .= "a sentence".$_POST['end']." with something in the END.";
}
I want to check the values in one function, in other words I want to check multiple values at the same time. I have seen few method but not sure which one is right, using comma or && or ||, something like below ...
if(!empty($_POST['start']) , (!empty($_POST['middle']) , (!empty($_POST['end']))
or
if(!empty($_POST['start']) && (!empty($_POST['middle']) && (!empty($_POST['end']))
or
if(!empty($_POST['start']) || (!empty($_POST['middle']) || (!empty($_POST['end']))
Can anyone tell me the right code for this kind of formation?
here are some basic.. i made it as a comment(as i was not sure if this is the thing you asked for) but i guess an answer would be appropriate with a bit of details.
the AND operatior
the && will check every condition and if all are true it will return true...
take it like this
if(FALSE && TRUE)
it will always return False and if will not execute because one of the condition is false
The OR operator
THe || will check the first condition if its true it will return true else check the second condition If all are false(not even a single is true) it will return false.
again following the previous example
if(TRUE || False || False)
now the compiler checks the first condition if its true it will ignore the next two conditions and return true.
if(FALSE || FALSE || FALSE) - this will return false as all are false
THe comma Operator
if you , operatior then the last condition to the right will be evaluated and if it is true then it will return true else false
example
if(True,True,True,False) - it will return false
if(FALSE, TRUE, FALSE, TRUE) - it will return true
so choose the operator according to your logic.
USE THIS :
if((!empty($_POST['start'])) && (!empty($_POST['start'])) && (!empty($_POST['start'])));
Your looking for something like:
// Establish valid post key values
$valid_post_variables = array_flip( ['start', 'middle', 'end'] );
// Fetch post data
$post = $_POST;
// $result will contain the values of post where the keys matched valid
$result = array_intersect_key( $post, $valid_post_variables );
// if the resulting array contains our 3 options, its go time
if ( count( $result ) == 3 ) {
//start middle and end where all passed via POST
}
function insertPost($before, $offset, $after)
{
if(!empty($_POST[$offset])) {
return $before . $_POST[$offset] . $after;
}
return '';
}
$description = insertPost('a sentence', 'start', ' with something in the START.');

Categories