Get folder from address

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
User avatar
waradmin
Forum Contributor
Posts: 240
Joined: Fri Nov 04, 2005 2:57 pm

Get folder from address

Post by waradmin »

Ok, so I want to write a redirect that uses the folder you are currently in as a value. To clear that up:

Say the user is in the folder myopenspace.net/steve

I want the redirect to take the steve (being the folder) and add that to the redirect

Code: Select all

<? 
   header('Location: http://www.myopenspace.net/user/?P=steve'); 
?>
So basicly the steve comes from the folder your in, if you were at http://www.myopenspace.net/testfolder the redirect would need to go to http://www.myopenspace.net/user/?P=testfolder

But im looking for this to be dynamic. When a user registers for the site, I want it to copy a index.php file into a directory that is created and the index.php will know to redirect to their profile by using the folder the file is in as the username. Is there a way to do this?

-steve
User avatar
John Cartwright
Site Admin
Posts: 11470
Joined: Tue Dec 23, 2003 2:10 am
Location: Toronto
Contact:

Post by John Cartwright »

viewtopic.php?p=233247#233247

Already answered this..
User avatar
waradmin
Forum Contributor
Posts: 240
Joined: Fri Nov 04, 2005 2:57 pm

Post by waradmin »

Im not looking for database use here at all, simply a way to without using a DB get the /_____ and put it in a redirect as /user/?P=_______

-steve
User avatar
nickman013
Forum Regular
Posts: 764
Joined: Sun Aug 14, 2005 12:02 am
Location: Long Island, New York

Post by nickman013 »

waradmin wrote:Im not looking for database use here at all, simply a way to without using a DB get the /_____ and put it in a redirect as /user/?P=_______

-steve
there is no database is that post that he told you to go to. it is exactly what you are looking for too.
josh
DevNet Master
Posts: 4872
Joined: Wed Feb 11, 2004 3:23 pm
Location: Palm beach, Florida

Post by josh »

As said before, mod_rewrite will do that for you
User avatar
waradmin
Forum Contributor
Posts: 240
Joined: Fri Nov 04, 2005 2:57 pm

Post by waradmin »

So what would the file then look like? Would that code be in the header for a redirect or what? I am new to the ModRewrite function.

Thanks for the help and sorry for the misunderstanding on the previous post.

-steve
User avatar
nickman013
Forum Regular
Posts: 764
Joined: Sun Aug 14, 2005 12:02 am
Location: Long Island, New York

Post by nickman013 »

This is exactly what you are looking for. It probly isnt coded the best that it could, and its probly horrible. But it works, and it is what you are looking for.

make this index.php in every username folder

index.php

Code: Select all

<?
$testArray = explode('/', $_SERVER['PHP_SELF']);
$url = "http://www.myopenspace.net/user/?P=$testArray[1]";
?>
<html>
<META HTTP-EQUIV="Refresh" CONTENT="0; URL=<? echo "$url";?>">
</html>
try it out!

EDIT: IGNORE THE BELOW
nt color="#000000"><?php
User avatar
waradmin
Forum Contributor
Posts: 240
Joined: Fri Nov 04, 2005 2:57 pm

Post by waradmin »

Nickman, thanks for that code, it solved many problems. Problem solved.

-steve
User avatar
nickman013
Forum Regular
Posts: 764
Joined: Sun Aug 14, 2005 12:02 am
Location: Long Island, New York

Post by nickman013 »

Cool, Thanks glad to help you.

I am glad I finally helped somebody and didnt get help from somebody. :D
User avatar
John Cartwright
Site Admin
Posts: 11470
Joined: Tue Dec 23, 2003 2:10 am
Location: Toronto
Contact:

Post by John Cartwright »

Your way your going to have to manually put an index file and user folder for every user..

Put the following in an .htaccess file

Code: Select all

RewriteEngine On
RewriteBase /

RewriteRule ^user/([A-Za-z]+)$ /user.php?P=$1
And then visiting the url http://domain.com/user/bob will transparantly point to user.php with $_GET['P'] as the username

A much cleaner solution IMO
User avatar
nickman013
Forum Regular
Posts: 764
Joined: Sun Aug 14, 2005 12:02 am
Location: Long Island, New York

Post by nickman013 »

^
indeed.
Post Reply