I want to output a message,
whenever the url includes any parameter that start with p2, for example in all the following instances:
example.com/?p2=hello
example.com/?p2foo=hello
example.com/?p2
example.com/?p2=
I've tried:
if (!empty($GET['p2'])) {
echo "a parameter that starts with p2 , is showing in your url address";
} else {
echo "not showing";
}
this should cover all your cases
$filtered = array_filter(array_keys($_GET), function($k) {
return strpos($k, 'p2') === 0;
});
if ( !empty($filtered) ) {
echo 'a paramater that starts with p2 , is showing in your url address';
}
else {
echo 'not showing';
}
Just iterate over the $_GET array and add a condition for the key to start with p2 when matching do what you need to do.
foreach($_GET as $key=>$value){
if (substr($key, 0, 2) === "p2"){
// do your thing
print $value;
}
}
substr($key,0,2) takes the first two characters from the string
try
if (isset($GET['p2'])) {
echo "a paramater that starts with p2 , is showing in your url address";
} else {
echo "not showing";
}
fastest way is
if(preg_match("/(^|\|)p2/",implode("|",array_keys($_GET)))){
//do stuff
}
Related
The problem is with this line:
if $var LIKE '1800%';
and I'm not sure how to fix it. Thanks.
<?php
//check to see if account number is like 1800*
if (isset($_POST['acct_number'])) {
$var = $_POST['acct_number'];
if $var LIKE '1800%'; {
//stop the code
exit;
} else {
echo 'normal account number';
}
}
?>
You need PHP not MySQL. For 1800% just check that it is found at position 0:
if(strpos($var, '1800') === 0) {
//stop the code
exit;
} else {
echo 'normal account number';
}
If it can occur anywhere like %1800% then:
if(strpos($var, '1800') !== false) {
//stop the code
exit;
} else {
echo 'normal account number';
}
Use substr function to get first 4 characters and compare it with 1800.
if(substr($var, 0, 4) == '1800')
{
// your code goes here.
}
``
Another way could be to use strpos()
if (strpos($var, '1800') === 0) {
// var starts with '1800'
}
I would use a regular expression for this preg_match('/^1800.+/', $search, $matches);
i need to use if statement inside my table
the value of the row is "Released" but i want to show Pending on my html table. how can i achieve it. this is my current code. what's wrong with my if statement?
if (strlen($row['signed']) == "Released") {
echo "Pending";
}
else{
echo $row['signed'];
}
strlen checks for string length. first check either signed is set in the array and then check if value is equal
if (isset($row['signed']) && $row['signed'] == "Released") {
echo "Pending";
}
else{
echo $row['signed'];
}
strlen() returns the length of the argument, so it returns an integer. You can check if value is equals to the string which you want something like this:
if ($row['signed'] == "Released") {
echo "Pending";
} else {
echo "Released";
}
strlen() is used to count the length of the string, to check if it matches "Released", just use == to compare:
if ($row['signed'] == "Released") {
echo "Pending";
} else {
echo $row['signed'];
}
To find out is $row['signed'] is set, just use isset():
if (isset($row['signed'])) {
echo "Pending";
} else {
echo $row['signed'];
}
More information on isset(): http://php.net/manual/en/function.isset.php
More information on PHP operators: http://php.net/manual/en/language.operators.php
Try this:
if ($row['signed'] == "Released") {
$value = "Pending";
} else {
$value = "Released"
}
And add <?php echo $value; ?> in your table
My foreach loop:
$empty = "You cannot decommission this truck. It is in use."
$msg = "";
foreach ($trucks_to_remove as $truck_id) {
$truck_id = trim($truck_id);
if (strlen($truck_id)) {
$msg .= decom_truck(
$db,
$depot_id,
$truck_id
);
}
else
{
echo $empty;
}
}
echo $msg;
If the condition is not met, it means the sql cursor that preceded this foreach loop did not return a result for this truck_id (meaning it is in use), I just want a simple message sent to the page to alert the user.
It does not have to be a pop-up or the like. Where would I put my write or print statement? I assume I'd use an else clause but when I add an else inside the foreach loop (see above), I get nothing printed to the page. I have also tried the following:
else if (strlen($truck_id)) == 0
{
echo $empty;
}
I am very new to php.
$arr1 = array();
if(!$arr1)
{
echo 'arr1 is emty<br/>';
$arr2 = array(1, 2);
if($arr2)
echo 'arr2 has data';
exit();
}
I have the following three possible urls..
www.mydomain.com/445/loggedin/?status=empty
www.mydomain.com/445/loggedin/?status=complete
www.mydomain.com/445/loggedin/
The www.mydomain.com/445 part is dynamically generated and is different each time so I can't do an exact match, how can i detect the following...
If $url contains loggedin but DOES NOT CONTAIN either /?status=empty OR /?status=complete
Everything I try fails as no matter what it will always detect the logged in part..
if(strpos($referrer, '?status=empty')) {
echo 'The status is empty';
}
elseif(strpos($referrer, '?status=complete')) {
echo 'The status is complete';
}
elseif(strpos($referrer, '/loggedin/')) {
echo 'The status is loggedin';
}
Slice up the URL into segments
$path = explode('/',$referrer);
$path = array_slice($path,1);
Then just use your logic on that array, the first URL you included would return this:
Array ( [0] => 445 [1] => loggedin [2] => ?status=empty )
You could do something like this:
$referrer = 'www.mydomain.com/445/loggedin/?status=empty';
// turn the referrer into an array, delimited by the /
$url = explode('/', $referrer);
// the statuses we check against as an array
$statuses = array('?status=complete', '?status=empty');
// If "loggedin" is found in the url, and count the array_intersect matches, if the matches = 0, none of the statuses you specified where found
if( in_array('loggedin', $url) && count(array_intersect($url, $statuses)) == 0 )
{
echo 'The user is logged in';
}
// if the complete status exists in the url
else if( in_array('?status=complete', $url) )
{
echo 'The status is complete';
}
// if the empty status exists in the url
else if( in_array('?status=empty', $url) )
{
echo 'The status is empty';
}
I would recommend looking at array_intersect, it is quite useful.
Hope it helps, not sure if this is the best way of doing it, but might spark your imagination.
Strpos is probably not what you want to use for this. You could do it with stristr:
if($test_str = stristr($referrer, '/loggedin/'))
{
if(stristr($test_str, '?status=empty'))
{
echo 'empty';
}
elseif (stristr($test_str, '?status=complete'))
{
echo 'complete';
} else {
echo 'logged in';
}
}
But it's probably easier/better to do it with regular expressions:
if(preg_match('/\/loggedin\/(\?status=(.+))?$/', $referrer, $match))
{
if(count($match)==2) echo "The status is ".$match[2];
else echo "The status is logged in";
}
I have a bizarre thing happening, bingbot is spidering my website, and one such URL looks like this:
https://xxxxx/programme.php?action=view&id=2233
In my code, I have this function:
function getNumericValue($value) {
if (is_numeric($value)) {
return mysql_escape_string($value);
}
if (isset($_GET[$value])) {
if (!is_numeric($_GET[$value])) {
echo "$value must be numeric.";
exit;
}
return mysql_escape_string($_GET[$value]);
} if (isset($_POST[$value])) {
if (!is_numeric($_POST[$value])) {
echo "$value must be numeric.";
exit;
}
return mysql_escape_string($_POST[$value]);
} else {
echo "Please specify a $value";
debug("Please specify a $value - " .$_SERVER['REQUEST_URI'].' : '.$_SERVER['HTTP_USER_AGENT'].' : '.print_r($_POST, true).' USERID: '.getUserid());
exit;
}
}
The debug() function emails me errors every 15 minutes, and when microsoft spider the site, I am getting: Please specify a id - /programme.php?action=view&id=2233 : msnbot-NewsBlogs/2.0b (+http://search.msn.com/msnbot.htm) : Array ( ) USERID: -1
You can see from the URL that it has an id, but PHP is totally ignoring it. It is fine for google spiders.
Any ideas what could be happening, and how to fix it?
Wild guess - maybe there's an extra space after id number and is_numeric('2233 ') returns false. Try trimming the value taken from $_GET/$_POST (or $_REQUEST for that matter):
function getNumericValue($value) {
if (is_numeric($value)) {
return mysql_escape_string($value);
}
if (isset($_REQUEST[$value])) {
$request_value = trim($_REQUEST[$value]);
if (!is_numeric($request_value)) {
echo "$value must be numeric.";
exit;
}
return mysql_escape_string($request_value);
}
echo "Please specify a $value";
debug("Please specify a $value - " .$_SERVER['REQUEST_URI'].' : '.$_SERVER['HTTP_USER_AGENT'].' : '.print_r($_POST, true).' USERID: '.getUserid());
exit;
}