can somone help explain this plz

General discussions related to php

Moderators: macek, egami, gesf

scorpage
New php-forum User
New php-forum User
Posts: 6
Joined: Thu Aug 14, 2003 2:41 am

can somone help explain this plz

Postby scorpage » Thu Aug 14, 2003 2:48 am

i have seen codes on site with stuff like:
index.php?links

how do u do that? i've tried it but no luck ...can somone help plz

User avatar
swirlee
Moderator
Moderator
Posts: 2272
Joined: Sat Jul 05, 2003 1:18 pm
Location: A bunk in the back
Contact:

Postby swirlee » Thu Aug 14, 2003 7:33 am

scorpage, assuming you already have a solid knowledge of PHP, this is pretty simple. The predefined variable $_SERVER is an array which contains information about the server and the request. The element $_SERVER['REQUEST_URI'] contains the request URI, which is essentially everything in the URL that comes after .com, .org, etc. So if, for example, if the user goes to:

http://yoursite.com/index.php?links

Then $_SERVER['REQUEST_URI'] will contain the string "/index.php?links". So, in order to make something meaningful out of this, you'll just want to grab everything after the '?'. The following function will do this:

Code: Select all

<?
   function get_args($string) {
      return substr($string, $strpos($string, '?') + 1);
   }
?>


So, if the user requests /index.php?links, the above will just return "links". You could feed it to a switch() statement to choose which action to do. Of course, you'll want to do lots of error-checking in case the user decides to alter the URL and try to foul things up.

scorpage
New php-forum User
New php-forum User
Posts: 6
Joined: Thu Aug 14, 2003 2:41 am

Postby scorpage » Thu Aug 14, 2003 9:48 am

sorry but i don't understand PHP that well can u explain in simple terms how i can use it plz

User avatar
swirlee
Moderator
Moderator
Posts: 2272
Joined: Sat Jul 05, 2003 1:18 pm
Location: A bunk in the back
Contact:

Postby swirlee » Thu Aug 14, 2003 10:39 am

scorpage wrote:sorry but i don't understand PHP that well can u explain in simple terms how i can use it plz


No, we cannot teach you PHP. There are innumerable web sites, tutorials, and books out there to do this. I could give you the basic framework for doing what you ask, but that would be doing doing you a disservice, as you wouldn't actually understand what you're doing. I'd like to recommend to you Google or Amazon.com.

not_a_bozo
New php-forum User
New php-forum User
Posts: 4
Joined: Wed Aug 06, 2003 4:27 pm

using "$_SERVER['REQUEST_URI']" where index.php is

Postby not_a_bozo » Thu Aug 21, 2003 7:04 am

Greetings, Swirlee! ...

I found just what I needed in my first php-forum.com site search. Turns out your answer to scorpage's question helps me, too! ...

Now, here's my follow-up question I hope you can offer help to:

A user enters "members.mysite.com/swirlee" ... and I want to pull up a web page which has its data stored in a database by parsing the element $_SERVER['REQUEST_URI'] - grabbing "swirlee" and essentially doing a SELECT * FROM database WHERE username is <swirlee>

Can this be done in the absence of the "index.php?" part of the URI? :lol:

thanks for your time and help with this.

respectfully,

not_a_bozo

User avatar
swirlee
Moderator
Moderator
Posts: 2272
Joined: Sat Jul 05, 2003 1:18 pm
Location: A bunk in the back
Contact:

Re: using "$_SERVER['REQUEST_URI']" where index.ph

Postby swirlee » Thu Aug 21, 2003 7:26 am

not_a_bozo wrote:Can this be done in the absence of the "index.php?" part of the URI? :lol:


By "in the absence of index.php?", do you mean just http://mysite.com/ or http://mysite.com/mydir/, without anything after? Sure you can do that, so long as your web server defaults to index.php in the absence of a filename. If you want to be able to do the SELECT based on user-passed parameters, though, it becomes a bit more complicated, because in the absence of variables passed by GET (after the ?), there are only two ways to pass variables: a form POST, or sessions. To do a form POST, obviously, the user needs to have submitted a form via POST (though there are interesting ways to work arond this). Otherwise, you're stuck with sessions, for which there are many, many tutorials available.

not_a_bozo
New php-forum User
New php-forum User
Posts: 4
Joined: Wed Aug 06, 2003 4:27 pm

I think you understand my needs ....

Postby not_a_bozo » Thu Aug 21, 2003 7:43 am

By "in the absence of index.php?", do you mean just http://mysite.com/ or http://mysite.com/mydir/, without anything after?

Yes I want to have someone simply enter a URL like "http://members.mysite.com/username" into the browser address bar and have the (default) "index.php" (located within the members.mysite.com directory) parse the URL, obtaining the 'username' and in turn I will be using that 'username' as input to a MySQL select statement.

I think you are saying that I must use sessions
because [of] the absence of variables passed by GET (after the ?)
... and that there are many tutorials on this available (which I will search for now) ...

You also mention
(though there are interesting ways to work arond this)
in the absence of a POST form submitted by the user.

Which option do you think, in your opinion/experience, would be the better option to choose? (given that I am not well-versed in PHP, but can read manuals and/or delve into some PHP code to discern the workings therein - to a degree) ...

thank you, Swirlee, for your promptness in responding to my original post!!!!!! much much appreciated.

not_a_bozo

User avatar
tryton
New php-forum User
New php-forum User
Posts: 49
Joined: Wed Jul 09, 2003 8:25 am
Location: Joze
Contact:

Postby tryton » Thu Aug 21, 2003 9:12 am

not_a_bozo

I'm fairly new to php myself (been working with it for about six months now) and as a beginner, I would have to say I've been using the GET option using a form. I just pass the variable from one page to another page that runs a query on my database and then loads the page according to what variable was passed. And as scorpage has stated, there are literally tons of tutorials out there. A place where you can find some good examples of what you are looking for can be found on http://www.phpfreaks.com They have great tutorials.

User avatar
swirlee
Moderator
Moderator
Posts: 2272
Joined: Sat Jul 05, 2003 1:18 pm
Location: A bunk in the back
Contact:

Re: I think you understand my needs ....

Postby swirlee » Thu Aug 21, 2003 11:54 am

not_a_bozo wrote:Yes I want to have someone simply enter a URL like "http://members.mysite.com/username" into the browser address bar and have the (default) "index.php" (located within the members.mysite.com directory) parse the URL, obtaining the 'username' and in turn I will be using that 'username' as input to a MySQL select statement.


Ah, now we're getting somewhere! Okay, there are potentially two ways to do this (well, there are more, but I can only tell you specific to Apache -- if you're not using Apache, I'm sorry for so many reasons). First, the easiest way, you can get mostly what you want by doing this instead:

http://members.mysite.com/index.php/username

Apache has a special function by which if it doesn't find a file, it'll backtrack along the URL until it finds one that does exist. So the above would load index.php. Then, you can access the variable $_SERVER['REQUEST_URI'], which will give you everything after the .com, e.g. "/index.php/username", and then you can parse that to get "username".

The alternate method, if you don't want that /index.php/ in your URL, is to use the Apache module called mod_rewrite. This is kind of complex to set up, but works great thereafter. It involves adding special directives to your httpd.conf file or your
a .htaccess file. Rather than try to teach you this, I'll just point you to a great tutorial:

mod_rewrite: A Beginner's Guide to URL Rewriting

Enjoy.

not_a_bozo
New php-forum User
New php-forum User
Posts: 4
Joined: Wed Aug 06, 2003 4:27 pm

Gona go the mod_rewrite

Postby not_a_bozo » Thu Aug 21, 2003 12:31 pm

The alternate method, if you don't want that /index.php/ in your URL, is to use the Apache module called mod_rewrite.
Swirlee, I'm gona go the mod_rewrite route! :D ... I've used it before in the site I am upgrading, and just need to renew my understanding of it. ... as I go along in doing this, I will certainly get back to this forum and write a follow-up (or ask for more help :wink: ) ... Oh, and, Yes, I am workin with an Apache server, so your concerns may be a bit aleviated I hope.

Again, thanks for your response(s). You were able to hit the nail on the head - asking the right questions
By "in the absence of index.php?", do you mean just http://mysite.com/ or http://mysite.com/mydir/, without anything after?
... and obviously hearing/listening to my situation. Kudos to you for your on-the-mark contributions!!!!

Ciao,
not_a_bozo

ps, thank you also for the link to the sitepoint.com article.


Return to “PHP General”

Who is online

Users browsing this forum: No registered users and 1 guest