how can I sort properly with preg_match #2? - php

It's me again trying to understand this preg_match usage.
( how can I sort properly with preg_match? )
Now I want to modify this line
if (($title <> "") && (strpos($link,"<") === false) && !preg_match("/wilmaa|tvsector/i",$page))
This line I learned ignores all the $page cells that contains wilmaa|tvsector, fine. I want to add to the screening procedure this
!preg_match("/18+/i",$lang) to screen out adult content .
I've tried all the possible combinations of && or || , I've put the ! inside and outside the () and never got it to work. All I received Is either 0 channels or the full 700 of them, tnx

You're running into a problem here because + is a special character in regex. You need to escape it.
if (($title <> "") && (strpos($link,"<") === false) && !preg_match("/wilmaa|tvsector/i",$page) && !preg_match("/18\+/", $lang))
Also, the i modifier is not needed. I indicates case-insensitive matches, which wouldn't apply for your search.

Related

How to combine AND and OR operators in a if statement?

I have the following strings that contains parts of a domain as string.
$scheme="http";
$host="example.com";
$filename="demo.php";
$request_uri="/folder/demo.php";
$host and $filename variables one of them is optional.
I am using the following code to check if strings contain the correct value, print the successful messege ,else print the error message
if($scheme ="http" OR $host ="example.com" & $filename ="demo.php" & $request_uri ="/folder/demo.php")
{echo "Sucessful";}
else
{echo "Fail";}
It doesnt work as expected, and I am getting "Successful" everytime . I think the AND oprator is being ignored in the if statement.
Any Idea?
if( $scheme == "http"
&& ($host == "example.com" || $filename == "demo.php")
&& $request_uri == "/folder/demo.php"
) {
echo "Sucessful";
} else {
echo "Fail";
}
&& and || are PHP's operators for AND and OR. Also = assigns a value to a variable, not comparing for equality. Use == or === for comparing.
Also, it seemed that your written statement of requirements did not match your code ... so I changed the logic a little and added () to group the logic. From the written it sounds like the $scheme AND $request_uri test were required and either $host or $filename were required. Your code didn't seem to match that. If I misunderstood your written requirment, change the && and || and () around as you require. Note that breaking long boolean expressions into multiple lines and the use of () to group logic often helps with getting it right.

IF with multiple conditions not working

I am trying to create an if statement with multiple conditions but it won't work and perhaps it is my syntax.
I have two post variables, which both give me the value fine elsewhere on my page. They are:
$_POST['text']
$_POST['rating'] //can be G, PG or R
What I am trying to do is make my word filter code work only if the rating equals "G"
What is currently happening though is that the filter is flagging a bad word regardless of the rating and ignoring my IF rating = G part.
if (isset($_POST['text']) and $_POST['rating'] = "G") {
//give warning if bad words are used
}
<?php
if (isset($_POST['text']) && $_POST['rating'] =="G") {
//give warning if bad words are used
}?>
use it like this
You may also use the symbol version of the syntax.
&& for and
|| for or
Also, = does not mean equals in an if statement. That is the syntax for setting a variable.
You would say == (equals to).
To say "not equals" you would use != or !=
if (isset($_POST['text']) && $_POST['rating'] == "G") {
//give warning if bad words are used
}

Weird error when writting OR conditionals in PHP

This error has been bugging me for a long time and I can't find an answer anywhere on the Internet, even using PHP official documentation.
When I write if statements with multiple conditions like this
if ((empty($user) == true) || (isset($user->data) == false)) {
//...
}
PHP says "Call to undefined function ()".
Then I try this alternative:
if (empty($user) == true || isset($user->data) == false) {
//...
}
And PHP says Call to undefined function isset().
PHP version 5.5.15.
By chance I just found the answer to my own problem. I cannot believe it, after all this time.
You're right #Musa
if (empty($user) == true || isset($user->data) == false) {
if (empty($user) == true || isset($user->data) == false) {
I realized something was wrong reconstructing both conditionals and looking at Sublime's syntax highlighting.
I use alt gr to write the pipe symbol and sometimes I left it pressed more than I should and I end up writting alt gr + space. It results in an invisible character that I believed to be a space.
Thanks everyone.
How about:
if (empty($user) || !isset($user->data)) {
//...
}

PHP if condition doesn't work for urls matching

I am trying to match two URLs but it seems doesn't work. I have tried works fine if I put manually.
Here is the code:
$referby = $_SERVER['HTTP_REFERER'];
$link1="http://domain.com/admin/ajax/passcodev.php?order_id=".$orderid;
$link2="http://www.domain.com/admin/ajax/passcodev.php?order_id=".$orderid;
if($referby<>$link1 || $referby<>$link2)
{
header('Location:passcodev.php?order_id='.$orderid);
}
I have no idea where I am doing mistake.
if($referby<>$link1 || $referby<>$link2) means if $referby does not match $link1 or $link2, proceed. Since it can't match both it always evaluates to true.
You need to use && (and):
if($referby != $link1 && $referby != $link2)

Displaying code only for certain countries

So I have an affiliate program that's lately been getting a lot of bad leads from countries we don't service, which we tell affiliates will be voided. Anyways, this is going to cause our reversal rate to be through the roof. So I want to do something about it.
So I want to use geoip, which gets me the stuff as a 2 letter country code.
<?php
$output = shell_exec('geoip-lookup '.$_SERVER['REMOTE_ADDR']);
?>
So how do I put that together?
Would this be correct?
<?php
$output = shell_exec('geoip-lookup '.$_SERVER['REMOTE_ADDR']);
if($_SESSION['check']!=1) && ($output=='US' || $output=='AU' || $output=='GB' || $output=='CA')
{
$submission = $_POST['submission_id'];
$_SESSION['check']=1;
echo 'insert pixel';
}
?>
You'll want to verify $output is what you expect (i.e. a string - var_dump($output)).
However, provided it is, you'll need to quote your country codes to do a string comparison. Also, as noted by heximal, be sure to use == for comparison. Otherwise, it's an assignment.
if ($_SESSION['check'] != 1 && ($output == 'US' || $output == 'AU' ...)) {
Otherwise, the rest of you code appears correct (without knowing your exact logic).
I am not familiar with "geoip-lookup" but I have "geoiplookup" working in my Ubuntu shell. Hopefully I can assume they are the same?
Putting $_SERVER values into a shell command has the potential for disaster, because they can be spoofed, so be careful with that.
Try the following:
// Make sure that the remote address is an IP and not something harmful, like "1.1.1.1; rm ../ -rf".
$found = preg_match('/^(?:\d{1,3}\.){3}\d{1,3}$/', $_SERVER['REMOTE_ADDR']);
if($found)
{
$command = escapeshellcmd('geoiplookup '.escapeshellarg($_SERVER['REMOTE_ADDR']));
$output = shell_exec($command);
if(($_SESSION['check']!=1) && (strpos($output,'US')!==false || strpos($output,'AU')!==false || strpos($output,'GB')!==false || strpos($output,'CA')!==false))
{
$submission = $_POST['submission_id'];
$_SESSION['check'] = 1;
echo 'insert pixel';
}
}
Be careful with $output=US
if you want to compare two operands you should use == operator

Categories