Click to See Complete Forum and Search --> : HasQueryString
whisher06
09-05-2007, 11:30 AM
Hi.
I want to check if there is a valid query
string in a URL.
<?php
error_reporting(E_ALL | E_STRICT);
function hasQueryString(){
$has= false;
$pos= strpos($_SERVER['REQUEST_URI'], "?");
$current= current($_GET);
if( ($pos !== false) && (count($_GET) > 0) && !empty($current)) {
$has= true;
}
return $has;
}
var_dump(hasQueryString());
echo "<br />";
?>
<a href="<?php $_SERVER['PHP_SELF']; ?>?">test</a><br />
<a href="<?php $_SERVER['PHP_SELF']; ?>?id">test</a><br />
<a href="<?php $_SERVER['PHP_SELF']; ?>?id=">test</a><br />
<a href="<?php $_SERVER['PHP_SELF']; ?>?id=1">test</a><br />
What do you think about ?
Best wishes.
laserlight
09-05-2007, 12:36 PM
I think that it is overly complex, and possibly incorrect. Would you consider ?id=0 to be an "invalid" query string?
I suspect that just using current($_GET) != '' should be enough. If there are no array elements, current($_GET) would return false, and false != '' is false. '0' != '' on the other hand, is true, so ?id=0 would be accepted.
whisher06
09-05-2007, 03:46 PM
Thanks a lot for the ready reply.
I think that it is overly complex, and possibly incorrect. Would you consider ?id=0 to be an "invalid" query string?
I suspect that just using current($_GET) != '' should be enough. If there are no array elements, current($_GET) would return false, and false != '' is false. '0' != '' on the other hand, is true, so ?id=0 would be accepted.
In my script I've got all the key with positive integer
but I agree with you
It's my first idea.
The present idea ;)
<?php
error_reporting(E_ALL | E_STRICT);
function parseQuery(){
$tmp= null;
parse_str($_SERVER['QUERY_STRING'],$output);
$current= current($output);
if(!empty($current)){
$tmp= $output;
}
return $tmp;
}
function isKeyAllowed($getValue,$allowedGet){
$isAllowed= false;
$userGetKey= array_keys($getValue);
if(is_array($userGetKey) && is_array($allowedGet)){
$isAllowed= in_array($userGetKey,$allowedGet);
}
return $isAllowed;
}
$allowedGet= array(array('d','m'));
if(is_null($userGetKey= parseQuery())){
echo 'index.php';
}
elseif(isKeyAllowed($userGetKey,$allowedGet)){
echo 'index.php?d=1&m=2';
}
else{
echo '404.php';
}
?>
<br />
<a href="<?php $_SERVER['PHP_SELF']; ?>?">test</a><br />
<a href="<?php $_SERVER['PHP_SELF']; ?>?id">test</a><br />
<a href="<?php $_SERVER['PHP_SELF']; ?>?id=">test</a><br />
<a href="<?php $_SERVER['PHP_SELF']; ?>?id=1">test</a><br />
<a href="<?php $_SERVER['PHP_SELF']; ?>?d=1&m=2">multi</a><br />
What do you think about ?
whisher06
09-06-2007, 12:41 PM
What's for:
<?php
class Request{
private $postData= array();
private $getData= array();
public function __construct(){
//Registry::set('request', $this);
$this->parseQuery();
}
public function getPost($key){
return isset($_POST[$key]) ? $_POST[$key] : null;
}
public function getGet($key){
return isset($this->getData[$key]) ? trim($this->getData[$key]) : null;
}
public function formHasPropriety(){
$hasProperty= true;
$args= func_get_args();
foreach($args as $arg){
if(is_null($value=$this->getPost($arg))){
$hasProperty= false;
}
else{
$this->postData[$arg]= trim($value);
}
}
return $hasProperty;
}
public function linkHasPropriety(){
$hasProperty= true;
$args= func_get_args();
foreach($args as $arg){
if(is_null($value=$this->getGet($arg))){
$hasProperty= false;
}
}
return $hasProperty;
}
public function getPostValue(){
return $this->postData;
}
public function getGetValue(){
return $this->getData;
}
private function parseQuery(){
$current= current($_GET);
if(!empty($current)){
parse_str($_SERVER['QUERY_STRING'],$this->getData);
}
}
public function redirect($page){
$host= $_SERVER['HTTP_HOST'];
$uri= rtrim(dirname($_SERVER['PHP_SELF']), '/\\');
$extra= $page;
header("Location: http://$host$uri/$extra");
exit();
}
public function refresh($page,$time=3){
$extra= $page;
header("Refresh: $time; URL=\"$extra\"");
}
final public function __destruct(){
unset($this->postData);
unset($this->getData);
}
}//
$request= new Request();
/* YOU MUST PUT THE POST REQUEST BEFORE */
if($request->formHasPropriety('title','check_insert','save')){
print_r($request->getPostValue());
}
elseif($request->formHasPropriety('title_','check_insert_','save_')){
print_r($request->getPostValue());
}
elseif($request->linkHasPropriety('y') && !$request->linkHasPropriety('m')){
echo "index?y=".$request->getGet('y');
}
elseif($request->linkHasPropriety('m','y') && !$request->linkHasPropriety('d')){
echo "index?m=".$request->getGet('m')."&y=".$request->getGet('y');
}
elseif($request->linkHasPropriety('d','m','y')){
echo "index?d=".$request->getGet('d')."&m=".$request->getGet('m')."&y=".$request->getGet('y');
}
elseif($request->linkHasPropriety('c')){
echo "index?c=".$request->getGet('c');
}
else{
echo "INDEX";
}
//var_dump($_POST);
?>
<br />
<a href="<?php $_SERVER['PHP_SELF']; ?>?">test</a><br />
<a href="<?php $_SERVER['PHP_SELF']; ?>?id">test</a><br />
<a href="<?php $_SERVER['PHP_SELF']; ?>?id=">test</a><br />
<a href="<?php $_SERVER['PHP_SELF']; ?>?id=1">test</a><br />
<a href="<?php $_SERVER['PHP_SELF']; ?>?y= 2007">Browse by year</a><br />
<a href="<?php $_SERVER['PHP_SELF']; ?>?m=1&y=2007">Browse by month</a><br />
<a href="<?php $_SERVER['PHP_SELF']; ?>?d=5&m=1&y=2007">Browse by day</a><br />
<a href="<?php $_SERVER['PHP_SELF']; ?>?c=5">Browse by Category</a><br />
<form action="<?php $_SERVER['PHP_SELF']; ?>" method="post" id="frm-tag">
<fieldset id="comment-1">
<label for="title">Comment : </label>
<input name="title" title="Comment name" type="text" value="" id="title-comment" /><br>
<input name="check_insert" type="hidden" value="1189086462" />
<label for="save"> </label>
<input name="save" type="submit" value="Save" id="save-comment" />
</fieldset>
</form>
<form action="<?php $_SERVER['PHP_SELF']; ?>" method="post" id="frm-tag">
<fieldset id="comment-2">
<label for="title">Comment : </label>
<input name="title_" title="Comment name" type="text" value="" id="title" /><br>
<input name="check_insert_" type="hidden" value="1189086462" />
<label for="save"> </label>
<input name="save_" type="submit" value="Save" id="save" />
</fieldset>
</form>
Just a simple controller ;)
Any comments on how I should be doing this better would be gratefully appreciated.
whisher06
09-06-2007, 03:22 PM
a little better:
public function formHasPropriety(){
$hasProperty= true;
$args= func_get_args();
foreach($args as $arg){
if(is_null($value=$this->getPost($arg))){
$hasProperty= false;
}
else{
$this->postData[$arg]= is_string($value)?trim($value):$value;
}
}
return $hasProperty;
}
Shrike
09-07-2007, 07:29 PM
This seems very complicated for what is really a simple task. Why not just use isset( $_GET['key'] )?
I think the Request object should be mostly ignorant of the actual method with which the request was done, since GET and POST should be mutually exclusive.
class Request
{
private $input;
public function __construct()
{
switch( $_SERVER['REQUEST_METHOD' )
{
case 'GET':
$this->input = $_GET;
break;
default:
$this->input = $_POST;
}
public function get( $key )
{
return !empty( $this->input[$key] ) ? $this->input[$key] : false;
}
}
Whats the point of re-parsing the request data when it's already been done?
You might also want to consider non-web tasks. In a CLI environment you'd want to parse the command line arguments. If you went to these lengths it would almost be worth abstracting the actual request data to it's own object, a la Strategy.
whisher06
09-18-2007, 09:11 AM
Thanks a lot buddy for the advice.
As usual a very smart way ......
:D
Bye.
PHP Builder
Copyright Internet.com Inc. All Rights Reserved.