LinuxQuestions.org
Welcome to the most active Linux Forum on the web.
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices

Reply
 
LinkBack Search this Thread
Old 10-11-2005, 05:35 PM   #1
benrose111488
Member
 
Registered: Jun 2004
Location: Wantage, NJ
Distribution: Fedora 7
Posts: 177

Rep: Reputation: 30
Parsing URL in PHP


Hey all

I want a feature on my website which will be easier to explain through examples:

If someone visits http://profile.speakfree.us/123456
I want the profile of the user with userid (in a MySQL table) 123456 to appear

If someone visits http://profile.speakfree.us/benrose111488
I want the profile of the user with that username (same place) to appear

Essentially, when someone types in either of these, I need to figure out a way to get the part past the "/" but before anything else (such as a ?) and assign that value to a variable.

But I have no idea how to do this since I always get a 404 when trying to reach those URLs since the server treats them as directories.

Any suggestions? Thanks much.

--Ben Rose
 
Old 10-11-2005, 07:03 PM   #2
clb
Member
 
Registered: Sep 2004
Location: UK
Distribution: Ubuntu
Posts: 117

Rep: Reputation: 16
I presume they are files as opposed to directories?

if they are files you will need to use a page.php?var=12345 querystring method to get the variable. If they are actually directories then try putting an index.php page inside, and using that to parse the URL, possibly using the __FILE__ constant.
 
Old 10-11-2005, 08:55 PM   #3
benrose111488
Member
 
Registered: Jun 2004
Location: Wantage, NJ
Distribution: Fedora 7
Posts: 177

Original Poster
Rep: Reputation: 30
They are neither files or directories. I want the user to be able to put that into their browser and have thet bring them to the correct file with the correct value filled in in the ?var=xxx
 
Old 10-12-2005, 02:15 AM   #4
clb
Member
 
Registered: Sep 2004
Location: UK
Distribution: Ubuntu
Posts: 117

Rep: Reputation: 16
My apologies, I misunderstood your first post. You need a script in the directory to handle the string.

If you place an index.php file into the directory you can do what you want by using the url http://path.to.domain/dir/?xxxxxxx
To do this you need to use the following code to grab the data:
PHP Code:
$string $HTTP_SERVER_VARS['QUERY_STRING'];
echo 
$string
Let me know if there are any problems, but this should be what you need.
 
Old 10-12-2005, 10:35 AM   #5
benrose111488
Member
 
Registered: Jun 2004
Location: Wantage, NJ
Distribution: Fedora 7
Posts: 177

Original Poster
Rep: Reputation: 30
But I don't want that question mark to be there. I know how to get vars in the URL but I want them to just be able to put in the username after the slash. What about an error:404 page that will direct them to the correct page?
 
Old 10-12-2005, 11:28 AM   #6
Hko
Senior Member
 
Registered: Aug 2002
Location: Groningen, The Netherlands
Distribution: ubuntu
Posts: 2,524

Rep: Reputation: 93
PHP Code:
<html>
<head>
    <title>Test URL-parsing</title>
</head>
<body>
<h2>Test URL-parsing</h2>
<?php
    $url 
"http://"$_SERVER['SERVER_NAME'] . $_SERVER['REQUEST_URI'];
    echo 
'<b>Full URL</b>: '$url .'<br />';
    
$part parse_url($url);
    echo 
'<b>Protocol: </b>'$part['scheme'] .'<br />';
    echo 
'<b>Host: </b>'$part['host'] .'<br />';
    echo 
'<b>Path: </b>'$part['path'] .'<br />';
    echo 
'<b>Query: </b>'$part['query'] .'<br />';
?>
</body>
</html>
 
Old 10-12-2005, 12:12 PM   #7
deiussum
Member
 
Registered: Aug 2003
Location: Santa Clara, CA
Distribution: Slackware
Posts: 895

Rep: Reputation: 31
If you are using Apache and have access to the config file, an alternate option might be to look into putting some form of directory handler in. I haven't ever done this myself so I don't know the exact syntax, but it's something you could investigate. You might need to have a subdirectory to specify for the handler, like users. So if you go to anything underneath the http://yourserver.com/users/ directory, it would get passed through a handler of your choice.

Something like so in your apache config for instance:

Code:
<Location /users>
    SetHandler Your-handler
</Location>
Disclaimer: I know nothing about adding new handlers in apache so the above might not even work, but it is something you could investigate further.

Last edited by deiussum; 10-12-2005 at 12:14 PM.
 
  


Reply


Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off
Trackbacks are Off
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
URL parsing program in perl ludeKing Programming 3 09-30-2010 03:58 PM
Mozilla Linux Command Line URL Parsing Security Flaw Reported win32sux Linux - Security 9 10-06-2005 06:39 AM
php parsing some includes. others are not. toadeny Linux - Newbie 4 09-18-2003 09:50 AM
php not parsing chens_83 Linux - General 9 02-19-2003 04:53 AM
auto url parsing on console bluecadet Linux - General 1 12-18-2001 04:07 AM


All times are GMT -5. The time now is 02:44 PM.

Main Menu
 
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
identi.ca: @linuxquestions
Facebook: @linuxquestions
Open Source Consulting | Domain Registration