Jump to content

Using Logical Operator with an IF


Skor

Recommended Posts

I'm trying to evaluate 2 different values in an IF statement -- basically allowing only authorized URLs to post to the page.  I'm able to get each URL to work individually, but I'm not able to get both to be evaluated.  Where would I put the OR.  Here's what I've got.

 

$ref = $_SERVER['HTTP_REFERER'];

If ($ref != "http://www.sitename.com/post1.php")
{ 
echo "<head><title>Invalid Request</title></head>";
echo "This operation cannot be performed.  Please go to <a href=\"http://www.sitename.com\">www.carolynkedesigns.com</a> to order. </font>";
}

 

I want to say:

 

 

If ($ref != "http://www.sitename.com/post1.php OR http://www.sitename.com/post2.php") but that didn't work and have been able to find an example of how to do this so I'm stuck.  Thanks in advance. 

Link to comment
https://forums.phpfreaks.com/topic/71130-using-logical-operator-with-an-if/
Share on other sites

<?php
If ($ref !== "http://www.sitename.com/post1.php" ||
$ref !==  "http://www.sitename.com/post2.php")
{ 
echo "<head><title>Invalid Request</title></head>";
echo "This operation cannot be performed.  Please go to <a href=\"http://www.sitename.com\">www.carolynkedesigns.com</a> to order. </font>";
}?>

if($ref == "http://www.sitename.com/post1.php" || $ref == "http://www.sitename.com/post2.php"){
//enter page
}else{
echo "Invalid Request";
}

 

if($ref != "http://www.sitename.com/post1.php" && $ref != "http://www.sitename.com/post2.php"){
echo "Invalid Request";
}else{
//enter page
}

I tried this and it didn't work. 

 

If ($ref != 'http://www.sitename.com/page1.php' || $ref != 'http://www.sitename.com/page2.php')

{

echo

 

Looks like the array solution only evaluates one value unless I'm incorrect here.

 

markjoe has got it .. not goes with and, and equals goes with or in this case.

 

Regarding the array solution, have you tried it?  It works with any number of values.  Every value in the array is checked.  The logic is inverted though - you are checking for equals rather than not equals.  It's the same logic as markjoe's first example.

<?php
$ref = $_SERVER['HTTP_REFERER'];

If ($ref != "http://www.sitename.com/post1.php" || $ref != "http://www.sitename.com/post2.php")
{ 
echo "<head><title>Invalid Request</title></head>";
echo "This operation cannot be performed.  Please go to <a href=\"http://www.sitename.com\">www.carolynkedesigns.com</a> to order. </font>";
}
?>

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.