php require minimum 3 words form validation - php

I have this php code for form validation :
if(($_FILES['file']['error'] != 0) || $title == '' || $tags == '') {
wp_redirect(home_url('/') . '?posterror=1');
exit;
}
This form redirect you to a page error if you didn't upload a file, put a title or a tag. From this code I want to change this: $tags == '' to something like this: $tags != 3 but it doesn't work, to require minimum 3 words = 3 tags.

You need to count the words, if they are separated by spaces, use
if(($_FILES['file']['error'] != 0) || $title == '' || str_word_count($tags) < 3) {
wp_redirect(home_url('/') . '?posterror=1');
exit;
}
if they are separated by comma-space ,, use this
if(($_FILES['file']['error'] != 0) || $title == '' || count(explode(", ", $tags)) < 3) {
wp_redirect(home_url('/') . '?posterror=1');
exit;
}

Related

how to use AND and OR together in php

Am trying to write a simple PHP function that uses both && and || function in php but am only able to use the && function, i get a desired result as shown with the below code
<?php
$srt = "asc";
if($srt != "" && $srt != "asc"){
echo "close";
} else {
echo "open";
}
?>
output >> open
But when i add the || function to it i get the wrong result
<?php
$srt = "asc";
if($srt != "" && $srt != "asc" || $srt != "desc"){
echo "close";
} else {
echo "open";
}
?>
output >> close
I have also try using AND in place of && and OR in place of || since php accepts both but i still get the same result am new to php so i dont know if what am trying is allowed or not so any advise will be greatly appreciated.
Use extra parenthesis to make it an expression.
if ($srt != "" && ($srt != "asc" || $srt != "desc"))
So you compare $srt != "" AND $srt != "asc" || $srt != "desc".
Your code can be improved for readability as
if (!in_array($srt, ['', 'asc', 'desc']))

why do I keep getting missing link errors with my email activation

I have done an email activation, which is working but am trying to adopt the same concept for my antispam activation. It is working for some people but sometimes, I keep getting that one of the variables is not set, is just just a trial and error thing?
I also read somewhere that I should include a not empty check too after the isset() check.
<?php
include_once __DIR__.'/header2.php';
if(isset($_SESSION['u_uid'])) {
echo "<meta http-equiv='refresh' content='0;url=../header2.php?reply=mustloggedoutfirst'>";
exit();
} else {
if(!isset($_GET['email']) && $_GET['email'] !== '' || !isset($_GET['userid']) && $_GET['userid'] !== '' || !isset($_GET['id']) && $_GET['id'] !== '' || !isset($_GET['reply_registration_expirydate']) && $_GET['reply_registration_expirydate'] !== '') {
echo "<meta http-equiv='refresh' content='0;url=../index.php?reply=missinglink'>";
exit();
} else {
$email = strip_tags($_GET['email']);
$username = strip_tags($_GET['userid']);
$id = strip_tags($_GET['id']);
$reply_registration_expirydate = strip_tags($_GET['reply_registration_expirydate']);
if (empty($_SESSION['key'])) {
$_SESSION['key'] = base64_encode(openssl_random_pseudo_bytes(32));
}
}
}
I forgot to add to my previous question that initially, I had the following code, which seems to work for most people for not for some...
if (!isset($_GET['email']) || !isset($_GET['activatetoken']) || !isset($_GET['reply_registration_expirydate'])) {
echo "<meta http-equiv='refresh' content='0;url=../index.php?reply=missinglink'>";
exit();
Update your IF-clause and remove the negation of "isset".
With "!isset($_GET['email']) && $_GET['email'] !== ''" you're checking if the variable is NOT set and then (being not set) if it's NOT unequal to an empty string, this must run into a "variable not set"-error.
The logical order of your if statement is probably incorrect. By "missing link errors" you probably mean this part of the if-statement is executed:
if(!isset($_GET['email']) && $_GET['email'] !== '' || !isset($_GET['userid']) && $_GET['userid'] !== '' || !isset($_GET['id']) && $_GET['id'] !== '' || !isset($_GET['reply_registration_expirydate']) && $_GET['reply_registration_expirydate'] !== '') {
formatted:
if (! isset($_GET['email']) // IS NOT SET email
&& $_GET['email'] !== '' // and IS NOT EMPTY email
|| !isset($_GET['userid']) // or IS NOT SET userid
&& $_GET['userid'] !== '' // and IS NOT EMPTY userid
|| !isset($_GET['id']) // or IS NOT SET id
&& $_GET['id'] !== '' // and IS NOT EMPTY id
|| !isset($_GET['reply_registration_expirydate']) // or IS NOT SET reply_registration_expirydate
&& $_GET['reply_registration_expirydate'] !== '') { // and IS NOT EMPTY reply_registration_expirydate
Due to the precedence of the operators, your statement might evaluate to true earlier than you want. You need to use parentheses to group operands.
if ((! isset($_GET['email']) // (IS NOT SET email
&& $_GET['email'] !== '') // and IS NOT EMPTY email)
|| (!isset($_GET['userid']) // or (IS NOT SET userid
&& $_GET['userid'] !== '') // and IS NOT EMPTY userid)
|| (!isset($_GET['id']) // or (IS NOT SET id
&& $_GET['id'] !== '') // and IS NOT EMPTY id)
|| (!isset($_GET['reply_registration_expirydate']) // or (IS NOT SET reply_registration_expirydate
&& $_GET['reply_registration_expirydate'] !== '')) { // and IS NOT EMPTY reply_registration_expirydate)

Search for Specific Text with Special Characters in a String in PHP

I am using a script to check if a string $status (randomly generated from another script which is interacting with an API) contains the following text(s) or not -
SC-102
EOD-103
EOD-31
DLYDC-104
EOD-72
ST-108
SC-104
X-SC
EOD-114
DLYMR-118
EOD-76
EOD-6
EOD-95
EOD-69
EOD-16
EOD-2
EOD-97
EOD-110
EOD-15
EOD-32
EOD-107
EOD-106
EOD-46
EOD-104
EOD-42
EOD-7
EOD-111
EOD-43
Can I use OR or || in my code ?
The Letters, Special Characters & Numbers are one single piece of TEXT.
For. Example - ST-102 is an exact text to be search from $status
My present code looks like the following -
$codes = array("SC-102","EOD-103","EOD-31","DLYDC-104","EOD-72","ST-108","SC-104","X-SC","EOD-114","DLYMR-118","EOD-76","EOD-6","EOD-95","EOD-69","EOD-16","EOD-2","EOD-97","EOD-110","EOD-15","EOD-32","EOD-107","EOD-106","EOD-46","EOD-104","EOD-42","EOD-7","EOD-111","EOD-43");
if($status_code==$codes)
{
//My Script , if condition is true!
}
Use explode and in_array function to achieve
<?php
$string = 'SC-102 || EOD-103 || EOD-31 || DLYDC-104 || EOD-72 || ST-108 || SC-104 || X-SC || EOD-114 || DLYMR-118 || EOD-76 || EOD-6 || EOD-95 || EOD-69 || EOD-16 || EOD-2 || EOD-97 || EOD-110 || EOD-15 || EOD-32 || EOD-107 || EOD-106 || EOD-46 || EOD-104 || EOD-42 || EOD-7 || EOD-111 || EOD-43';
$codes = explode(' || ',$string);
$status_code = 'ST-108';
if(in_array($status_code, $codes)){
echo "Code Found\n";
}else{
echo "Code Not Found\n";
}
$status_code = 'ST-102';
if(in_array($status_code, $codes)){
echo "Code Found\n";
}else{
echo "Code Not Found\n";
}
?>
live demo : https://eval.in/824205
You can do like this
$codes = array("SC-102","EOD-103","EOD-31","DLYDC-104","EOD-72","ST-108","SC-104","X-SC","EOD-114","DLYMR-118","EOD-76","EOD-6","EOD-95","EOD-69","EOD-16","EOD-2","EOD-97","EOD-110","EOD-15","EOD-32","EOD-107","EOD-106","EOD-46","EOD-104","EOD-42","EOD-7","EOD-111","EOD-43");
$status_code = 'ST-108';
if(in_array($status_code,$codes))
{
//My Script , if condition is true!
}
I thing it will help you .You can Check Here
Edit:-
Just change your if statement to
if(in_array($status_code,$codes))
Please take a look at the given script-
$codes = array("SC-102","EOD-103","EOD-31","DLYDC-104","EOD-72","ST-108","SC-104","X-SC","EOD-114","DLYMR-118","EOD-76","EOD-6","EOD-95","EOD-69","EOD-16","EOD-2","EOD-97","EOD-110","EOD-15","EOD-32","EOD-107","EOD-106","EOD-46","EOD-104","EOD-42","EOD-7","EOD-111","EOD-43");
$status_code = 'ST-108';
if(in_array($status_code, $codes)){
echo 'Found';
}else
echo 'Not Found';
you can search one value whether that exist in array or not.When you have string with || then you need to explode and then use in_array.Try below:
$values="SC-102 || EOD-103 || EOD-31 || DLYDC-104 || EOD-72 || ST-108 || SC-104 || X-SC || EOD-114 || DLYMR-118 || EOD-76 || EOD-6 || EOD-95 || EOD-69 || EOD-16 || EOD-2 || EOD-97 || EOD-110 || EOD-15 || EOD-32 || EOD-107 || EOD-106 || EOD-46 || EOD-104 || EOD-42 || EOD-7 || EOD-111 || EOD-43";
$codearr=explode("||",$values);
foreach($codearr as $status_code){
if(in_array($status_code,$codes)){
//found
}
else{
//not found
}
}

Exclude form fields from POST request

I have a simple foreach statement grabbing all the fields from a submitted form. The fields are then put into a table that is sent via email. There are 3 fields that I need submitted and validated, but not sent with the email. Here is my script generating the table:
foreach($_POST as $name => $value) {
if($name !== "num1" || $name !== "num2" || $name !== "captcha") {
$text .= "<tr><td>$name</td><td>";
//if form field is array
if(is_array($value)) {
foreach($value as $symptom) {
$text .= $symptom . ", ";
}
//else no array
} else {
$text .= $value;
}
$text .= "</td></tr>";
}
}
Basically all form field names are set as $name and all values are set as $value. I am trying to include all fields except the ones named num1, num2, & captcha.
I am not redoing the way this form is handled, I just need to exclude these 3 fields from the table but my conditional statement isn't doing what I had expected. How can I ensure these three fields won't be included in the email?
change if($name !== "num1" || $name !== "num2" || $name !== "captcha")
to if($name !== "num1" && $name !== "num2" && $name !== "captcha")
Change the || in your if() to &&. Your if() as written will ALWAYS succeed, because whatever value you're testing will always "not be" any of those values at the same time.
PHP's continue will skip an interation of a loop, then you change to using == comparison operators... Try this:
if($name == "num1" || $name == "num2" || $name == "captcha")
continue;
else {
// rest of your code
}
You should use && instead of || in if statement.

How do I extract a single column from another database?

Why this condition passes even if I change the $_GET variable?
I've this code
elseif(isset($_GET['results']) && $_GET['results'] == 'reorder' &&
isset($_GET['sort_column']) && $_GET['sort_column'] != '' && isset($_GET['sort_order'])
&& $_GET['sort_order'] != '' && $_GET['sort_order'] == 'asc'
|| $_GET['sort_order'] == 'desc') { /*rest goes here*/ } else {redirect}
Link returns like this
http://localhost/system/results.php?script_id=2&results=reorder&sort_column=supplier_address&sort_order=desc
But when I change this sort_column=supplier_address to say for example sorcodsalumn=supplier_address it doesn't redirect, instead goes ahead, any idea why? But if I simply remove few letters and dont replace with something else it does redirect...
How come if am using this isset($_GET['sort_column'] and am modifying sort_column to something else still passes this condition
Basic PHP operator precedence... && evaluates before ||, so your entire statement boils down to:
(x && y && z && ....) || ($_GET['sort_order'] == 'desc')
You need to simplify that if(), add some () to enforce your own evaluation order, and then things should start working a bit better.
your AND's and OR's need to be bracketed properly.
else if (isset($_GET['results']) &&
$_GET['results'] == 'reorder' &&
isset($_GET['sort_column']) &&
$_GET['sort_column'] != '' &&
isset($_GET['sort_order']) &&
$_GET['sort_order'] != '' &&
($_GET['sort_order'] == 'asc' || $_GET['sort_order'] == 'desc'))
{
/*rest goes here*/
} else {
redirect
}
More specifically your last || needs its own brackets, as shown above.
You need to put a bracket around your || (OR) statement like this:
elseif(isset($_GET['results']) && $_GET['results'] == 'reorder' &&
isset($_GET['sort_column']) && $_GET['sort_column'] != '' && isset($_GET['sort_order'])
&& $_GET['sort_order'] != '' && ($_GET['sort_order'] == 'asc'
|| $_GET['sort_order'] == 'desc')) { /*rest goes here*/ } else {redirect}
Otherwise your statement will return true anytime sort_order is set to 'desc'.

Categories