can somone help explain this plz

General discussions related to php

Moderators: egami, macek, gesf

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

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: 2257
Joined: Sat Jul 05, 2003 1:18 pm
Location: A bunk in the back
Contact:

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.
Image

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

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: 2257
Joined: Sat Jul 05, 2003 1:18 pm
Location: A bunk in the back
Contact:

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.
Image

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

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.
Image

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

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.
JCCorner.com - Online marketplace including online mall, link trading post, web articles and community board.

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

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.
Image

Post Reply
  • Information
  • Who is online

    Users browsing this forum: No registered users and 2 guests