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;
}
Related
Normally I use $_GET to catch the URL parameters and display content dependend on what the parameter is.
<?php
function isCmd($cmd) {
if(isset($_GET["cmd"]))
if($_GET["cmd"] == $cmd)
return TRUE;
return FALSE;
}
if(isCmd("page1")) {
echo "<p>That is page 1</p>";
}
else if(isCmd("page2")) {
echo "<p>That is page 2</p>";
}
else if(isCmd("page3")) {
echo "<p>That is page 3</p>";
}
else {
echo "<p>That is the normal page</p>";
}
The url would look like these
example.com/script.php
example.com/script.php?cmd=page1
example.com/script.php?cmd=page2
example.com/script.php?cmd=page3
And it works as expected, but I see on websites they can do just:
example.com/script.php?page1
example.com/script.php?page2
example.com/script.php?page3
and it would work, without cmd=
How can I do the same in PHP?
I answered it myself after looking into the $_GET supervariable. It was easier than I thought.
function isCmd($cmd) {
if(isset($_GET["$cmd"]))
return TRUE;
return FALSE;
}
if(isCmd("page1")) {
echo "<p>That is page 1</p>";
}
else if(isCmd("page2")) {
echo "<p>That is page 2</p>";
}
else if(isCmd("page3")) {
echo "<p>That is page 3</p>";
}
else {
echo "<p>That is the standard page</p>";
}
Also works with $_GET.
It would work with $_SERVER["QUERY_STRING"] too.
You can use $_SERVER["QUERY_STRING"] which will return the string after the "?" in the URL.
I need help.
As I do?
String same other string with different number.
Example
if ($current_server == "Lobby-01") {
echo "visiting in Lobby"
} elif ($current_server == "Lobby-02"){
echo "visiting in Lobby"
} and more..
I thought about trying
if ($current_server == "Lobby-/[0-99]+/"){
echo "visiting in Lobby"
} else {
//is false, then it will not show the message
}
is correct?
How do I do it?
Can you help me?
Use preg_match:
if (preg_match("/\bLobby-[0-9]{2}\b/i", $current_server)) {
echo "visiting in Lobby";
}
else {
// no message
}
Demo
This is a repeat question of Check $_POST['id'] is numeric then do this if not do otherwise
But..
The answers given do not answer the question IMHO.
The question is testing a $_POST string to see if it is a number or contains non-numeric characters. Not change the code to use $_GET.
The original question is as such:
if (!empty($_POST['id'])) {
echo "empty";
} else {
if (is_numeric($_POST['id'])) {
echo "numeric!";
} else {
echo "not empty but not numeric how come?";
}
}
I have tried, is_numeric() , but since $_POST delivers the variable as a string, even if it is a number this is useless.
I have tried, if(!preg_match('#[^0-9]#',$id)), but I have no idea why this doesn't work.
ctype_digit() I believe still has a problem with seeing all things from $_POST as strings, so no good.
There has to be a way, but I'm lost on how to do this. And no I can't use $_GET.
UPDATE ANSWER!!! (TYPO!!!!! god dammit!!!!):
// test.php
// testing with $_POST['id'] from forum with id = 5 and another test where id = new
$id = $_POST['editid'] ;
echo "<br>---".$id."---<br>";
if (empty($id)) {
echo "<br>1: empty";
} else {
if (!is_numeric($id)) {
echo "<br>2: This is the number 5";
} else {
echo "<br>3: the must be the word new";
}
}
// test 2 ... ctype_digit
if (empty($id)) {
echo "<br>4: empty";
} else {
if (!ctype_digit($id)) {
echo "<br>5: This is the number 5";
} else {
echo "<br>6: the must be the word new";
}
}
// test 3 ...
if (empty($id)) {
echo "<br>7: empty";
} else {
if (!preg_match('#[^0-9]#',$id)) {
echo "<br>8: This is the number 5";
} else {
echo "<br>9: the must be the word new";
}
}
/**
result from 5
---5---
3: the must be the word new
6: the must be the word new
8: This is the number 5
results from "new"
**/
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
}
my questions:
$state=array("你"=>1);
if(array_key_exists("你",$state))
{
$result = array_search("你",$state);echo $result;
}else
{
echo "No Exists";
}
i expect the result of "1", however the output is "No Exists", i don't know why the program can't get the value of the key "你".
array_search will search the given array by value. Try the following:
$state = array("你"=>1);
if(array_key_exists("你", $state)) {
echo $state["你"];
} else {
echo "No Exists";
}
// => 1
» demo
Hope below function will help.
<?php
$array = array('arr1'=>array('find_me'=>'yes you did.'));
function get_value_by_key($array,$key)
{
foreach($array as $k=>$each)
{
if($k==$key)
{
return $each;
}
if(is_array($each))
{
if($return = get_value_by_key($each,$key))
{
return $return;
}
}
}
}
echo get_value_by_key($array,'find_me');
?>
the encoding type of the show paper and the store paper is GB2312.
$state=array("你"=>1);
if(array_key_exists("你",$state))
{
$result1 = $state["你"];
echo $result1; // can get the value 111
}else
{
echo "No Exists";
}
the code above can be executed rightly. i can't show my problems accurately. Now i paste out my code , there is some questions.
<?php
$file = file("GB2312-HanZiBianMa.txt"); // file encoding type ANSI
foreach ($file as $line_num => $line)
{
list($no,$hex,$dec) = preg_split('[\t]',htmlspecialchars($line));;
$result[$hex] = $dec;
}
$result_2 = array_flip($result);
if(array_key_exists("你",$result_2)) // **can't find the value** 222
{
$state= $result_2["你"];
echo $state."<br/>";
}else
{
echo "No Exists<br/>";
}
foreach($result_2 as $k=>$each) //can get the value using the preg_match
{
if(preg_match('/你/', $k))
echo $k."\t".$each."<br/>";
}
?>
the format of GB2312-HanZiBianMa.txt is as follows:
1947 c4e3 你
1948 c4e4 匿
1949 c4e5 腻
1950 c4e6 逆
if your want to test the code , you can save the php code and save the GB2312.. file.
the question is:
why can't the following function get the right value ? the data comes from file and one stores together.
if(array_key_exists("你",$result_2)) // **can't find the value** 222
{
$state= $result_2["你"];
echo $state."<br/>";
}else
{
echo "No Exists<br/>";
}