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
}
Related
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.
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)
I have an input box that with php will echo out names typed in. The problem is that when the user presses space it will echo out the blank character. I have searched and didnt find an absolute answer. I know that using !empty if there is absoutely anthing in the input field but if there is a space is null supposed to work.
How to avoid getting echo if there is blank space in input?
if (!empty($name['name']) || null ) {
echo 'Your name is '.$name;
}
{
//do nothing
}
Calls to the function empty() will be true if it is unset, null, empty string, boolean false, or 0. Still I like avoiding heavy logic in conditionals... pull out the trimming and prepping the string before testing.
$nameString = empty($name['name'])?null:trim($name['name']);
if(!empty($nameString){
//go to town here
}
To appease the fanatics in advance, I have to also add that you should escape any user input before printing it. That way you protect against XSS. To be safer add try this:
$nameString = empty($name['name'])?null:trim($name['name']);
$nameString = htmlentities($nameString, ENT_QUOTES);
if(!empty($nameString){
//go to town here
}
However, a side note... the htmlentities() call does not protect against SQL injection, so if this data is going into a db you'll have to do more work--since your question doesn't indicate you're doing anything else than printing out to screen, we can hold off the SQL injection discussion for another day.
You want to check if there is a space, you may want to do this:
if ($name['name'] != null && trim($name['name']) != "" && !strpos(trim($name['name']), " "))
{
// work with $name['name']
}
else
{
// nothing
}
Be careful using empty(), it will return TRUE if the user enters '0'.
You might want to use strlen()
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.
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