Forum Moderators: coopster

Message Too Old, No Replies

working with query strings

         

cesarpo

9:32 pm on Mar 24, 2008 (gmt 0)

10+ Year Member



Hello,

i dont know the name of this, so i will go to the point:

i have

http://www.example.com/sometext

i want a way to get "sometext" in a variable in php, so every time a user type, the uri + "sometext" i can redirect that to a php scritp and use it.

i've seen this in lots of sites but in unable to find how to do it,
for example: <snip>

thanks!

sorry for my english, im learning :)

[edited by: eelixduppy at 10:24 pm (utc) on Mar. 24, 2008]
[edit reason] no URLs, please see charter [/edit]

eelixduppy

6:04 am on Mar 25, 2008 (gmt 0)



hello, and Welcome to WebmasterWorld! :)

You can use the $_GET superglobal array [us2.php.net] strings from the uri, however, the format will be slightly different--something more like the following:


http://www.example.com/?var=sometext

And to get this from the URI you would need to use $_GET['var'] in your code.

The format you have shown in your example, however, could still be used in the same method with GET, however, you are going to also have to incorporate some form of mod_rewrite [httpd.apache.org]. For more information or help regarding mod_rewrite, please refer to our Apache forum [webmasterworld.com] as that is the appropriate place for such questions.

dreamcatcher

7:45 am on Mar 25, 2008 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



You could also explode the $_SERVER['REQUEST_URI'] var and work with that:

$array = explode("/", $_SERVER['REQUEST_URI']);
print_r($array);

dc

databank

9:18 pm on Mar 25, 2008 (gmt 0)

10+ Year Member



you can do that with Apache's Rewrite Engine
is extremly used for SEO by forums, blogs, etc.

this is not a php thing
what you need it to do the following:
create a file named .htaccess
add following content inside (without #s):
###############################
RewriteEngine On
RewriteBase /
RewriteRule ^([a-zA-Z0-9\-\_]+)$ index.php?param=$1 [L]
###############################
upload this file to root folder, where your index.php is
and have whaterer is after / from index.php as $_GET['param']

this RewriteRule only accepts a-z, A-Z, 0-9, - and _
use the rule ^(.*)$ to match any character after /

hope this helps

cesarpo

5:34 am on Mar 26, 2008 (gmt 0)

10+ Year Member



yes it helps!
was the thing i was looking for!

i'll have learn this regular expression stuff..

thanks!