Log in

View Full Version : PHP URL Code



Dotayuri
01-26-2011, 11:01 PM
i been looking around and asking many people but no one ever knows how to do this

i need a php code that gets the users info by the url like this
www.mysite.com/members/Admin/1/
or
www.mysite.com/members/Admin/
or this
www.mysite.com/members/1/
but if someone enters on of the last to
it just takes them to
www.mysite.com/members/Admin/1/


ok so what im asking is
i need a php code that tells whos profile its in by those URLS



the php code i got only reads it if the url is like this
www.mysite.com/members.php?id=1

do any of you know how i can get this ?

djr33
01-26-2011, 11:07 PM
If you are using an apache-based server* (almost all linux servers, some windows servers), you can do this using a .htaccess file and mod_rewrite, an apache module. A more general term that's sometimes used is "pretty URLs".

Basically what it does is this: it takes the request URI then serves a different page. In other words, it pretends that your pages are structured differently than they are.

You create a series of rules and then output a "normal" URL that PHP can understand like ?id=1.

There's a lot of information out there. Just search for "mod rewrite". But be aware that it's very difficult to use, at least if you can't find a tutorial that matches exactly what you plan to do. The rules are based in regex (regular expressions) so learning a bit about that may help as well.

Here's a tutorial I wrote for using a somewhat simplified version of .htaccess for those who understand PHP well. It's a bit messy, but it should be a good place to start, at least if you know PHP well. If not, it might be just as easy to learn mod_rewrite itself.
http://www.dynamicdrive.com/forums/showthread.php?t=51923


(*If you are using another kind of server, there are similar alternatives, but I don't know much about them. You can probably find out by searching for mod_rewrite + server type because mod_rewrite is by far the most popular.)

Dotayuri
01-26-2011, 11:29 PM
thanks i dont really know much php but cants you just give me the code i need and tell me were to put it ?

i mean i all readly told you what i need it to do :P

djr33
01-28-2011, 04:40 AM
My tutorial above is meant for people who do know PHP but don't know mod_rewrite (yet). It sounds like that's not the best fit for you, so the alternative I suggest is learning about mod_rewrite.

You will create a .htaccess file. You will need to create some rules so that you can make your server treat it like you want.

Here's an example from my site for your reference. Your .htaccess file will look something like this, but you might need different rules.

RewriteEngine On
RewriteBase /somelocation/
RewriteCond %{REQUEST_URI} !^/somelocation/index.php$
RewriteCond %{REQUEST_URI} !^/somelocation/index.php/
RewriteRule ^(.*)$ index.php?$1 [L]

That basically says:
1. Use the "somelocation" directory as a base for this rule.
2-3. Check if the requested URI is not already index.php (to prevent a loop).
4. If those conditions are met, then take whatever was requested (specified by ^(.*)$) and add it to index.php? (via the variable "$1").

This might actually work for you, but you'll have to do some error checking in the PHP to make sure the submitted value is right, or at least have a default if you don't find a match.

If this does not match exactly what you need, you'll need to look into this yourself or maybe hire a designer if you need a lot of help. Generally, we're happy to help here, but if you need a project done for you then that's beyond free help (in most cases). Also, issues with mod_rewrite can be extremely hard to fix (lots of little details), especially without direct access to the server to do trial and error testing.