I am trying to do if statement with OR but it wont work
$ref = $_SERVER['HTTP_REFERER'];
$refData = parse_url($ref);
if($refData['host'] !== 'website1.com','website2.com') {
die("...");
}else{
<content>
}
The above code says unexpected syntax ','
Another one I tried is
$ref = $_SERVER['HTTP_REFERER'];
$refData = parse_url($ref);
if($refData['host'] !== 'website1.com' OR $refData['host'] !== 'website2.com') {
die("...");
}else{
<content>
}
The above returns ... even though requested from the correct page
Also when there is no OR in the code with one it works fine but not when trying to add the second.
What am I doing wrong?
UPDATE: Couldn't get this working no reason why so just set CURLOPT_REFERER on website2 as website1 and and kept it as
if($refData['host'] !== 'website1') {
thank you everyone
Consider using in_array.
$ref = $_SERVER['HTTP_REFERER'];
$refData = parse_url($ref);
$array_data = ["website1.com","website2.com"];
if(in_array($refData, $array_data))
{
echo "Do Something!";
}
else
{
echo "Failed!";
}
Replace !== to != and use && or OR = || depending of your need
$ref = $_SERVER['HTTP_REFERER'];
$refData = parse_url($ref);
if($refData['host'] != 'website1.com' || $refData['host'] != 'website2.com') {
die("...");
}else{
echo 'hi testing';
//run your php code here
}
You problem is that you are checking if both are not identical
`!=` ( Not equal)
`!==` (Not identical)
&& example $x && $y True if both $x and $y are true
|| example $x || $y True if either $x or $y is true
||
Is the OR operator in PHP.
But here, you need an AND operator &&
Like this :
if($refData['host'] !== 'website1.com' && $refData['host'] !== 'website2.com')
Related
I´m using strpos two timer. In the first if/else it works well but in the second it doesn´t work. This is my code:
if (strpos($word, "mono") == true) {
$type = "Monobloc";
} else {
$type = "Articulated";
}
if ($word, "galva") == true) {
$coating = "Galvanized Rod";
} elseif (strpos($word, "epoxi") == true) {
$coating = "EPOXI 100%";
} elseif ($word, "electro") == true) {
$coating = "Electrozinced";
}
Example:
If the variable word has the value "galva-mono" $type should be "Monobloc" and $coating should be "Galvanized Rod". The problem is that it is assigning well the $type but in the coating it doen´t enter in the if clause.
As stated in the official documentation:
Warning
This function may return Boolean FALSE, but may also return a
non-Boolean value which evaluates to FALSE. Please read the section on
Booleans for more information. Use the === operator for testing the
return value of this function.
You are checking the result with == true instead of !== false.
So, try this code:
if (strpos($word, "mono") !== false) {
$type = "Monobloc";
} else {
$type = "Articulated";
}
if (strpos($word, "galva") !== false) {
$coating = "Galvanized Rod";
} elseif (strpos($word, "epoxi") !== false) {
$coating = "EPOXI 100%";
} elseif (strpos($word, "electro") !== false) {
$coating = "Electrozinced";
}
I've been searching and I've tried multiple ways with no luck, What's happening in my code looks for a link. It will then determine what type of link it is and adjust it to suit my needs. Once adjusted I need it to go into an Array with the end goal of adding that Array to my database. print_r($items) will be where ill be adding the array to the Database if you know how do do this also I will not say no to the help :D I am guessing it the same as adding a variable to the database.
Here is my code so far :)
//Lets look for links
$urlcomp = array();
$items = array();
foreach($html->find('a') as $element){
if( strpos( $element->href, "http" ) !== false) {
$urlcomp[] = $element->href;
// Look for gov websites
if ((strpos($urlcomp, 'gov') !== false) || (strpos($urlcomp, 'police') !== false) || (strpos($urlcomp, 'nhs') !== false) || (strpos($urlcomp, 'org') !== false) || (strpos($urlcomp, 'council') !== false)){
} else {
if (in_array($urlcomp, $websiteurlall)) {
}else{
echo "First Batch - " . $urlcomp;
$items[] = array($urlcomp);
echo "</br>";
}
}
}else{
$urlcomp = $websiteurlcomp.$element->href;
// Look for gov websites
if ((strpos($urlcomp, 'gov') !== false) || (strpos($urlcomp, 'police') !== false) || (strpos($urlcomp, 'nhs') !== false) || (strpos($urlcomp, 'org') !== false) || (strpos($urlcomp, 'council') !== false)){
} else {
if (in_array($urlcomp, $websiteurlall)) {
}else{
echo "Second Batch - " . $urlcomp;
$items[] = array($urlcomp);
echo "</br>";
}
}
}
}
print_r($items);
$urlcomp[] = $element->href; should have been $urlcomp = $element->href; that was the only issue :S
I have the following code:
if ($Type != "DEA" and $VA != "Allowed" and $VolSess != 1) {
$max_rows = max($CMSReg_num_rows);
if ($max_rows == 0) {
mail($to, $subject, $body);
header('Location: '.bloginfo('home_url').'/profile');
}
}
The problem I have is that that an email is sent despite the if-statement being false, and only an email is sent. The rest of the code is not executed, i.e. no redirect. And when I comment out the mail() function, it does not send the email.
And when I add this code:
if ($VA == "Allowed") {
echo "VA = " . $VA;
}
if ($VolSess == 1) {
echo "VolSess = " . $VolSess;
}
I get this output:
VA = Allowed VolSess = 1
So I know that the condition in the if statement is false.
AND has a different order of precedence compared to &&. So your expression does not evaluate as you expect it to.
("$Type" != "DEA" and $VA != "Allowed" and $VolSess != 1)
should be
(("$Type" != "DEA") and ($VA != "Allowed") and ($VolSess != 1))
or
("$Type" != "DEA" && $VA != "Allowed" && $VolSess != 1)
for it to work as you expect it. This is one of those tiny mistakes/bugs that's easy to overlook.
try do an else after...
elseif($VA == "Allowed"){}
Try using the WordPress wp_mail().
die; after header() and also add 302 as a second argument to the header() function.
Enable error reporting with ini_set('display_errors', true); error_reporting(-1); on top of your PHP code.
Tell us what you see after making these changes.
Try:
if ($Type != 'DEA' && $VA != 'Allowed' && $VolSess != 1)
{
$max_rows = max($CMSReg_num_rows);
if ($max_rows === 0)
{
mail($to, $subject, $body);
header('Location: ' . bloginfo('home_url') . '/profile');
}
}
EDIT
The above works, but so does the oringal question code... The problem is elsewhere.
<?php
$Type = 'foo';
$VA = 'Allowed';
$VolSess = 1;
if ($Type != 'DEA' and $VA != 'Allowed' and $VolSess != 1)
{
$max_rows = 0;
if ($max_rows === 0)
{
echo 'Orig True';
}
}
else
{
echo 'fine?';
}
if ($Type != 'DEA' && $VA != 'Allowed' && $VolSess != 1)
{
$max_rows = 0;
if ($max_rows === 0)
{
echo 'Second True';
}
}
else
{
echo 'fine?';
}
?>
Both print 'fine?' Implying your error is elsewhere in your code.
I've written a function to make menu roll down if the page is the same as I declare.
The function looks like this
function menu_current()
{
$current = basename($_SERVER['REQUEST_URI']);
if ($current === "index?p=config" || "index?p=maintenance")
echo "class=\"nav-top-item suballowed current\" ";
else
echo "class=\"nav-top-item suballowed\" ";
}
It works perfectly if I only declare 1 page
if ($current === "index?p=config")
but not more. How to solve that solution? And is there a way to declare all websites between || tags in one variable instead of writing them like I did?
Replace this
if ($current === "index?p=config" || "index?p=maintenance")
with
if ($current === "index?p=config" || $current === "index?p=maintenance")
otherwise PHP doesn't know what should be equal to index?p=maintenance
You can use your approach if you set both sides of the equality check every time:
if ($current === "index?p=config" || $current === "index?p=maintenance") { ...
Perhaps a more "readable" solution:
if (in_array($current, array( 'index?p=config', 'index?p=maintenance' )) { ...
Another option would be to use a switch statement with a default.
function menu_current()
{
$current = basename($_SERVER['REQUEST_URI']);
switch($current) {
case "index?p=config":
case "index?p=maintenance":
echo "class=\"nav-top-item suballowed current\" ";
break;
default:
echo "class=\"nav-top-item suballowed\" ";
}
}
Here is the correct code
function menu_current()
{
$current = basename($_SERVER['REQUEST_URI']);
if ($current == "index?p=config" || $current == "index?p=maintenance")
echo "class=\"nav-top-item suballowed current\" ";
else
echo "class=\"nav-top-item suballowed\" ";
}
What I am trying to do is to display a message (1) if the current link does not contain the words "index" or "/?"
I found this to do the direct opposite:
$page = 'http://' . $_SERVER['SERVER_NAME'] . $_SERVER['REQUEST_URI'];
if (strpos($page, 'index.php') !== false xor strpos($page, '/?') !== false) {
echo '1';
} else {
echo '2';
}
This code displays "2" on pages where there is no "index" or "/?" in the link, but I need the opposite: display "1" where there is no "index" or "/?" in the link.
BTW I have tried all combinations: !strpos, TRUE, !==, but it doesn't seem to work for me. I need a way without the "else" in the code, otherwise I could just change up the echos.
$page = 'http://' . $_SERVER['SERVER_NAME'] . $_SERVER['REQUEST_URI'];
if (strpos($page, 'index.php') === false && strpos($page, '/?') === false)
{
echo"1";
}
else
{
echo"2";
}
Should display 1 if there's no index.php or /? in $page
The condition in your if statement is
if( <<COND 1>> xor <<COND 2>> )
where, xor's feature is
return TRUE if either <<COND 1>> or <<COND 2>> is TRUE, but not both.
You should instead use this:
if( strpos($page, 'index.php') !== false OR strpos($page, '/?') !== false )
Check this page for more information about logical operators.
$page = 'http://' . $_SERVER['SERVER_NAME'] . $_SERVER['REQUEST_URI'];
if (strpos($page, '/?') === false && strpos($page, 'index.php') === false)
{
echo "1";
}
else
{
echo "2";
}
Try this :
$page = 'http://' . $_SERVER['SERVER_NAME'] . $_SERVER['REQUEST_URI'];
if (strpos($page, 'index.php') === false || strpos($page, '/?') === false)
{
echo"1";
}
else
{
echo"2";
}