should i use $_SERVER['REQUEST_URI'] ?

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
original89
Forum Newbie
Posts: 22
Joined: Wed Nov 17, 2004 8:25 am

should i use $_SERVER['REQUEST_URI'] ?

Post by original89 »

Hi

I am currently using

if ($_SERVER['REQUEST_URI'] == "/thingy_browse.html")

to generate meta tags and descriptions on the fly that are related to the contents of this page but i would like to be able to create them for more specific results. As with most php things i pass some variables in the search string eg.

/thingy_browse.html?type=candles&clour= red


how is possible to target only the type and the colour and pass these results to the page, so whenever someone does a search on candles, i could have a bt of text that says.

''You searched on candles and we have lots of candles to choose from here.''


I know i could create a if ($_SERVER['REQUEST_URI'] for every combination but this would take forever, i already have a meta_tag.php page that is 5000 lines long and its becoming to big to handle.

Is there a way i could read in the possible variables i use in the searches and echo them onto the page and use them in the meta tags?.

many thanks for your suggestions etc...
Last edited by original89 on Mon Feb 27, 2006 2:43 pm, edited 2 times in total.
User avatar
s.dot
Tranquility In Moderation
Posts: 5001
Joined: Sun Feb 06, 2005 7:18 pm
Location: Indiana

Post by s.dot »

hmm i'm not really clear on what you want to do here

but maybe something like this?

Code: Select all

echo $_SERVER['PHP_SELF'].'?'.$_SERVER['QUERY_STRING'];
Set Search Time - A google chrome extension. When you search only results from the past year (or set time period) are displayed. Helps tremendously when using new technologies to avoid outdated results.
original89
Forum Newbie
Posts: 22
Joined: Wed Nov 17, 2004 8:25 am

Post by original89 »

hi

thanks for that reply well im not too sure what it is im asking for because im not too sure either..

i think you might be on the right lines though but i think i might have a problem using php_self as i re-write my php pages into .html which is why i have been using $_SERVER['REQUEST_URI']

what i really want is to be able to do is include the text from the query string in the page somewhere to help my SEO a bit, and not rely on having to write a script that interprets all the combinations of a dynamic page..so if my query string is

?type-candles&colour=red
then i could have a bit of text that says,,
'''youve found the best page in the world for buying red candles.''

but in another case it could be
?type-candles&colour=gold
''youve found the best page in the world for buying gold candles.''

or maybe the string might be just
?type-candles
''youve found the best page in the world for buying candles.''

i just wondered if there was a way i could set the text just for the search elements (is this what $_SERVER['QUERY_STRING']; does) rather than writing a massive if statement, which is already at 5000 lines..

does that make more sense? im sorry im a bit of a newbie, you can probably tell..

thanks v m
original89
Forum Newbie
Posts: 22
Joined: Wed Nov 17, 2004 8:25 am

Post by original89 »

okay ive pointed myself in the right direction would i just include this in the search results page.?


<?php echo $_SERVER['QUERY_STRING']; ?>


and my text would be

Youve arrived the the best place in the world to buy <? echo $_REQUEST['colour']; ?> <? echo $_REQUEST['type']; ?>
User avatar
RobertGonzalez
Site Administrator
Posts: 14293
Joined: Tue Sep 09, 2003 6:04 pm
Location: Fremont, CA, USA

Post by RobertGonzalez »

$_SERVER['QUERY_STRING'] will show you the entire string after the '?' in the URI. If you echo that you will see something like...

type=candles&color=red

To get the values for each var in the query string, use $_GET (you could use $_REQUEST, but that is not as secure as $_GET, in my opinion).

Code: Select all

if ( isset($_GET) )
{
    $type = '';
    $color = '';

    if ( isset($_GET['type']) )
    {
        $type = $_GET['type'];

        if ( isset($_GET['color']) )
        {
            $color = $_GET['color'];
        }
    }

    echo "You have selected $color $type.<br />";
}
original89
Forum Newbie
Posts: 22
Joined: Wed Nov 17, 2004 8:25 am

Post by original89 »

hi this seems to have done the trick

thanks very much..

although i have one query string that just doesnt want to work.

in the url its like this

type%5B%5D=cone

Code: Select all

if ( isset($_GET['type%5B%5D']) ) 
        { 
            $type= $_GET['type%5B%5D']; 
        }
and also

Code: Select all

if ( isset($_GET['type[]']) ) 
        { 
            $type= $_GET['type[]']; 
        }

but neither work only when i manually change the url from my posted form to


type[]=cone

will it work..

whats goign on, why is that one variable doing this and where does it get the [] from??


thanks for the help Everah and scrotaye, ive been stuck on this for a while, good job someone invented forums...
original89
Forum Newbie
Posts: 22
Joined: Wed Nov 17, 2004 8:25 am

Post by original89 »

do i need to decode it or something the [] one will not get written and it returns Array

or is there anything i can do to stop the [] appearing in the first place it comes froma drop down box on a form from posted from a previous page..

many thanks.

:(
MinDFreeZ
Forum Commoner
Posts: 58
Joined: Tue Feb 14, 2006 12:28 pm
Location: Lake Mary, FL

Post by MinDFreeZ »

the %5b is [ and %5d is ]

yea.. (edit) you've already got that figured out. :oops:
original89
Forum Newbie
Posts: 22
Joined: Wed Nov 17, 2004 8:25 am

Post by original89 »

sorted it, i just declare it as the first array

Code: Select all

if ( isset($_GET['type']) ) 
        { 
           $aac_type = $_GET['type'][0]; 
       } 
  }
and it works..

thanks me.. :lol:
original89
Forum Newbie
Posts: 22
Joined: Wed Nov 17, 2004 8:25 am

Post by original89 »

okay, its working but i have a little problem

when i have a search result where not all of the $_GET variables appear in the url then the echo doesnt work at all! they both have to be returned to make this work, otherwise nothing shows....

any ideas?

Code: Select all

if ( isset($_GET) ) 
{ 
    $type = ''; 
    $color = ''; 

    if ( isset($_GET['type']) ) 
    { 
        $type = $_GET['type']; 

        if ( isset($_GET['color']) ) 
        { 
            $color = $_GET['color']; 
        } 
    } 

    echo "You have selected $color $type.<br />"; 
}
how do you get the echo to work even if just one $_GET has been returned..
User avatar
Christopher
Site Administrator
Posts: 13596
Joined: Wed Aug 25, 2004 7:54 pm
Location: New York, NY, US

Post by Christopher »

How about

Code: Select all

if ( isset($_GET) ) 
{ 
    $type = ''; 
    $color = ''; 

    if ( isset($_GET['type']) ) 
    { 
        $type = $_GET['type']; 

        if ( isset($_GET['color']) ) 
        { 
            $color = $_GET['color']; 
        } 
    } 

    echo "You have selected $color $type.<br />"; 
}  else {
    echo "You have not selected anything.<br />"; 
}
Or:

Code: Select all

$type = 'nothing'; 
$color = ''; 

if ( isset($_GET) ) 
{ 
    if ( isset($_GET['type']) ) 
    { 
        $type = $_GET['type']; 

        if ( isset($_GET['color']) ) 
        { 
            $color = $_GET['color']; 
        } 
    } 

}
echo "You have selected $color $type.<br />";
(#10850)
User avatar
RobertGonzalez
Site Administrator
Posts: 14293
Joined: Tue Sep 09, 2003 6:04 pm
Location: Fremont, CA, USA

Post by RobertGonzalez »

EDIT: Sorry, screwed up the echo message part in the original...

Try something like this...

Code: Select all

<?php
$type = '';
$color = '';
$message = 'You have made no selections yet.<br />';

if ( isset($_GET) )
{
    if ( isset($_GET['type']) )
    {
        $type = $_GET['type'];
    }

    if ( isset($_GET['color']) )
    {
        $color = $_GET['color'];
    }

    if ( $type || $color )
    {
        $message = "You have selected $color $type.<br />";
    }
} 

echo $message;
?>
Post Reply