Jump to content

Get multiple parameters from URL and echo them out.


Recommended Posts

here's my code thus far I am stuck

<?php
$protocol = strpos(strtolower($_SERVER['SERVER_PROTOCOL']),'https') 
               === FALSE ? 'http' : 'https';
$host     = $_SERVER['HTTP_HOST'];
$script   = $_SERVER['SCRIPT_NAME'];


$query  = explode('&', $_SERVER['QUERY_STRING']);
$params = array();


foreach( $query as $param )
{
    list($name, $value) = explode('=', $param);
    $params[urldecode($name)][] = urldecode($value);
}



$currentUrl = $protocol . '://' . $host . $script . '?' . $query . '&' . $name . $value;
?>

See the $_GET array. It already contains the parsed key/value pairs, unless you are doing something unusual, which you didn't show. You also didn't state what incorrect result you are getting vs. what you expect that leads you to believe that you are stuck.

$protocol = strpos(strtolower($_SERVER['SERVER_PROTOCOL']),'https') 
                                === FALSE ? 'http' : 'https';

The SERVER_PROTOCOL will never contain "https". It is always in the form "HTTP/1.x". Check $_SERVER["HTTPS"] ("on" or "off") or, if that's not available, the less reliable $_SERVER["SERVER_PORT"] (80=http, 443=https).

 

Speaking of, you don't take into account the port number.

 

As for $currentUrl, $name and $value do not have what you want them to have. They're only useable inside the foreach loop.

ok I am trying to just get a name and value into the url string like index.php?Brand&Device .. then I echo it out as <?php

echo $params;

?>.gif" to put a picture in the section of the page which i pull from the url string currently it displays $array in all the fields

foreach($query as $param){
list($name, $value) = explode('=', $param);
$params[urldecode($name)] = urldecode($value); //for array purposes
$params_pairs[]=$param; //for string purposes
}

//Here we go with nice key=>value array
print'<pre>';
print_r($params);
print '</pre>';


$currentUrl = $protocol . '://' . $host . $script . '?' . implode('&', $params_pairs);

 

However I put implode for learning purposes as I think you doing all that stuff for this reason. To get only names, just iterate through keys with foreach.

 

However please be more precise when asking.

 

P.S. Also I'm not sure as I haven't any SSL server, but maybe isset($_SERVER['HTTPS']) is a good way to check https.

 

Edit: ok, requinix was more detailed with HTTPS, quite interesting :). Thanks.

Edited by BagoZonde

ok let me be more specific. I have one section on my site where the brand logo is displayed. and another section where the device image would be displayed.

the brand images are in .png format and the device are in .gif format. So i figured i could achieve it with this script.

this so far works for index.php?brand it will let me echo out the $params in my text i just need one extra

like this index,php?brand&device but I am not sure I can do it with the multiple parameters script i posted in here earlier or if it can be done period.

hope that clears up why i am trying to do this it has its purpose.

<?php
$protocol = strpos(strtolower($_SERVER['SERVER_PROTOCOL']),'https') 
               === FALSE ? 'http' : 'https';
$host     = $_SERVER['HTTP_HOST'];
$script   = $_SERVER['SCRIPT_NAME'];
$params   = $_SERVER['QUERY_STRING'];

$currentUrl = $protocol . '://' . $host . $script . '?' . $params;
?>

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.