note 77687 added to function.http-build-query

From: Date: Sat, 08 Sep 2007 20:28:18 +0000
Subject: note 77687 added to function.http-build-query
Groups: php.notes 
Request: Send a blank email to php-notes+get-130319@lists.php.net to get a copy of this message
This is my own version that I was using for php <=4, hope that will help someone this can accomplish a few things: if called w/o parameters will return the current link if called with the first parameter like: param1=a&param2=b will return a link with the query string containing ONLY what is passed. if called with the first parameter like: &param1=a&param2=b will return a string with the current query string plus what is passed to the function this function uses by default PHP_SELF, but if you pass the page will create the link with what you pass. If pass secure(boolean), will create an https or http. $url will be the actual domain. This function will use a global variable if nothing is passed, but feel free to modify it to use the _SERVER variables. $html is a boolean. If true will create links with &amp; else just & <?php function create_link($query=NULL, $page=NULL, $secure=NULL, $html=NULL, $port=NULL, $url=NULL ){ if($html === NULL) $html = true; if($url === NULL){ if($secure==true){ $url = $GLOBALS['_cfg']['secure_site']; } else { $url = $GLOBALS['_cfg']['url']; } } if($query === NULL) $query = $_SERVER['QUERY_STRING']; if($port === NULL && isset($_SERVER['BIBIVU-HTTPD'])){ $port === _SERVER_ADMIN_PORT; } if((isset($_SERVER['BIBIVU-HTTPD']) || !isset($_COOKIE[session_name()])) && $this->is_crawler()===false){ $query = $query.($query!=''?'&':'').session_name().'='.session_id(); } if(substr($query,0,1) == '&'){ $query = $this->change_query(substr($query,1)); } if($page === NULL) $page = $_SERVER['PHP_SELF']; $page = str_replace('//','/',$page); if(substr($page,0,1)=='/') $page = substr($page,1); $newQry = array(); if($query!=''){ parse_str($query, $newQuery); foreach($newQuery as $key => $item){ $newQry[] = $key.'='.$item; } } if($html){ //I create the URL in HTML $query = implode('&amp;', $newQry); } else { $query = implode('&', $newQry); } if(isset($_SERVER['BIBIVU-HTTPD'])){ $host = ''; } elseif(defined('_ADMIN_BIB') && _ADMIN_BIB==1){ if(isset($_SERVER['HTTPS']) && $_SERVER['HTTPS']=='on' && ($secure===NULL || $secure===true )){ $host = 'https://'; } else { $host = 'http://'; } if (strrpos($_SERVER['HTTP_HOST'], ':') > 0){ $host .= substr($_SERVER['HTTP_HOST'], 0, strrpos($_SERVER['HTTP_HOST'], ':')); } else { $host .= $_SERVER['HTTP_HOST']; } } else { if($secure==true){ $host = 'https://'.$url; } else { $host = 'http://'.$url; } } if($port==NULL){ //check the current port used $port = $_SERVER['SERVER_PORT']; if($port<=0) $port = 80; } if($port!=80 && $port!=443){ $host .=':'.$port; } if($page===''){ $ret = $query; } else { $ret = $host.'/'.$page.($query!=''?'?'.$query:''); } return $ret; } function change_query($addto_query, $queryOld = NULL){ // change the QUERY_STRING adding or changing the value passed if ($queryOld === NULL){ $query1 = $_SERVER['QUERY_STRING']; } else { $query1 = $queryOld; } parse_str ($query1, $array1); parse_str ($addto_query, $array2); $newQuery = array_merge($array1, $array2); foreach($newQuery as $key => $item){ $newQry[] = $key . '=' . $item; } return implode('&', $newQry); } ?> ---- Server IP: 69.147.83.197 Probable Submitter: 75.180.199.34 ---- Manual Page -- http://www.php.net/manual/en/function.http-build-query.php Edit -- https://master.php.net/note/edit/77687 Del: integrated -- https://master.php.net/note/delete/77687/integrated Del: useless -- https://master.php.net/note/delete/77687/useless Del: bad code -- https://master.php.net/note/delete/77687/bad+code Del: spam -- https://master.php.net/note/delete/77687/spam Del: non-english -- https://master.php.net/note/delete/77687/non-english Del: in docs -- https://master.php.net/note/delete/77687/in+docs Del: other reasons-- https://master.php.net/note/delete/77687 Reject -- https://master.php.net/note/reject/77687 Search -- https://master.php.net/manage/user-notes.php

« previous php.notes (#130319) next »