943,735 Members | Top Members by Rank

Ad:
  • PHP Discussion Thread
  • Marked Solved
  • Views: 1042
  • PHP RSS
You are currently viewing page 1 of this multi-page discussion thread
May 25th, 2009
0

how do i get a variable from a regular ol url?

Expand Post »
im trying to get the username of the personal profile.
the url is example.com/theusername/index.php
the "theusername" i want to make into a variable.
How do i do that? i want the variable to be called "theusername"
so how do i make that into a variable? does anyone know?
with php
cause i want to plug each individual variabl in to the rest of the code so its unique and can show the users information of the profile being viewed. i dont know any other way?
does anyone else?
there will be more than one profile obviously.(with different usernames ex. example.com/difusername)
or is there a simpler way to plug in the users information to be written on the page?
Last edited by Ezzaral; Jun 11th, 2009 at 2:36 pm. Reason: Changed to non-promotional url.
Reputation Points: 15
Solved Threads: 7
Posting Pro in Training
SKANK!!!!! is offline Offline
428 posts
since Apr 2009
May 25th, 2009
0

Re: how do i get a variable from a regular ol url?

Actually i didnt get u completely.
Anyway im just trying to help you
you can get username in many ways like using url (GET method), by creating session, cokkies etc or by getting it from database.
I prefer get or session to solve this.

If you are little more specific im sure there will be many more to help you.
Last edited by danishbacker; May 25th, 2009 at 3:11 am.
Reputation Points: 10
Solved Threads: 7
Junior Poster in Training
danishbacker is offline Offline
97 posts
since Apr 2008
May 25th, 2009
0

Re: how do i get a variable from a regular ol url?

I think what you are looking for is .htaccess files which is part of the apachie rewrite module. With .htaccess files, you can make fake urls that on the server side rewrite to the real file.
PHP Syntax (Toggle Plain Text)
  1. Example - real url:
  2. site.com/index.php?username=cwarn23&id=blog
  3. Fake url:
  4. site.com/cwarn23/blog/
I would provide some code but I just need to know your real url is including what goes after the ? symbol.
Sponsor
Featured Poster
Reputation Points: 410
Solved Threads: 258
Occupation: Genius
cwarn23 is offline Offline
3,004 posts
since Sep 2007
May 25th, 2009
0

Re: how do i get a variable from a regular ol url?

Here is sample code for you.

file1.php
======
PHP Syntax (Toggle Plain Text)
  1. <?php
  2. $no=$_REQUEST["no"];
  3. $name=$_REQUEST["name"];
  4.  
  5. echo "values - $no and $name";
  6. ?>
  7.  
  8. <br/>First Method
  9.  
  10. <form method="post" action="file1.php">
  11. No <input type="text" name="no"/>
  12. Name <input type="text" name="name"/>
  13. <br/><input type="submit" namd="cmd" value="Submit"/>
  14. </form>
  15.  
  16. <br/>Second Method
  17. <a href="file1.php?no=100&name=Mr.X">Send</a>
Moderator
Reputation Points: 2136
Solved Threads: 1228
Posting Genius
adatapost is offline Offline
6,527 posts
since Oct 2008
May 25th, 2009
0

Re: how do i get a variable from a regular ol url?

It looks like you need htaccess code. This will make a rule on your webserver to call a server file (I'll call it user.php) with details from your url.
PHP Syntax (Toggle Plain Text)
  1. RewriteEngine On
  2. RewriteRule ^/user/([A-Za-z0-9]+)/?$ user.php?username=$1

With this code in user.php username would be a GET variable you could use. Also, I would suggest making the URL's /user/username/ instead because lets say a user called themself index.php. Then this code would redirect that to their page. There is a way around this but I would require me to know every page on your site. It's just easier to use the /user/username structure.
Last edited by FlashCreations; May 25th, 2009 at 11:38 am.
Reputation Points: 47
Solved Threads: 47
Posting Whiz
FlashCreations is offline Offline
393 posts
since Sep 2008
May 25th, 2009
0

Re: how do i get a variable from a regular ol url?

I can't add anything to the stuff that is said about the .htaccess. But I got you a bit different.

You have an index file in a folder and you now need the name of the folder. This can be done as follows:

PHP Syntax (Toggle Plain Text)
  1. $uri=$REQUEST_URI;
  2. $tmp=explode("/",$uri);
  3. echo $tmp[$tmp.length()-2];

$REQUEST_URI returns the URL of your script including all folders relativ to root directory of your files. You can then find the folder where index.php is in.

Hope that helps.


PS When you try to do something similar to FB, where you can access a user via facebook.com/username I would do it as well as said before.
Reputation Points: 56
Solved Threads: 29
Posting Whiz in Training
sDJh is offline Offline
255 posts
since Aug 2005
May 25th, 2009
0

Re: how do i get a variable from a regular ol url?

That is true sDJh, but wouldn't this mean that spank would have to create a folder and index.php for every user the registers? Still though, I like your solution (My other plan was using almost exactly your idea except the URL would be /user.php/username but that wasn't exactly what was required).
Reputation Points: 47
Solved Threads: 47
Posting Whiz
FlashCreations is offline Offline
393 posts
since Sep 2008
May 25th, 2009
0

Re: how do i get a variable from a regular ol url?

ok . i already have the registration script making the username the person choses as their own directory which is for me right now because my name is thissucks
example.com/thissucks
i also made an index file in each which is the profile.
i kind of understand the code...
PHP Syntax (Toggle Plain Text)
  1. $uri=$REQUEST_URI;
  2. $tmp=explode("/",$uri);
  3. echo $tmp[$tmp.length()-2];
but i dont know how to use it.
i need to be able to have a variable.

which has to be called $specific_user

and this variable has to CHANGE for every profile that is being viewd
example: on example.com/thissucks (or example.com/thissucks/index.php)
$specific_user has to equal the word thissucks

example: on example.com/heywhats-up (or example.com/heywhats-up/index.php)
$specific_user has to equal the word heywhats-up

so how do i get $specific_user to change for every profile bein viewed ?

cause i have to use $specific_user to look up the information in the mysql database so the specific user's info will be on their profile..

??? does anyone get what i dont know what todo?


&&&&
i used the above code just put it in the test file right with php tags around it and i got this error:

Fatal error: Call to undefined function length() in /home/peace/public_html/test1.php on line 14
Last edited by Ezzaral; Jun 11th, 2009 at 2:38 pm. Reason: Changed to non-promotional url.
Reputation Points: 15
Solved Threads: 7
Posting Pro in Training
SKANK!!!!! is offline Offline
428 posts
since Apr 2009
May 25th, 2009
0

Re: how do i get a variable from a regular ol url?

Here is your code for the specific user variable. You must understand that for each user you need to create a new folder with an index.php containing the same code each time.
PHP Syntax (Toggle Plain Text)
  1. $tmp=explode("/",$REQUEST_URI);
  2. $specific_user = $tmp[count($tmp)-2];
Reputation Points: 47
Solved Threads: 47
Posting Whiz
FlashCreations is offline Offline
393 posts
since Sep 2008
May 25th, 2009
0

Re: how do i get a variable from a regular ol url?

ok so i put that code on the page and echoed the variable but nothing came up?
yeah i am fwriting the the page be the exact same code each time someone completes registration.
Last edited by SKANK!!!!!; May 25th, 2009 at 9:20 pm.
Reputation Points: 15
Solved Threads: 7
Posting Pro in Training
SKANK!!!!! is offline Offline
428 posts
since Apr 2009

This thread is solved

Either the thread starter or a moderator has marked this thread as solved. You can most likely trust the responses and answers given. There is most likely no reason for any further responses to be posted here. If you have a related question, please start a new thread in this forum instead.

This thread is more than three months old

No one has posted to this discussion for at least three months. Please let old threads die and do not reply to them unless you feel you have something new and valuable to contribute that absolutely must be added to make the discussion complete. Otherwise, please start a new thread in this forum instead.
Message:
Previous Thread in PHP Forum Timeline: OsCommerce: I cannot add products via the admin console
Next Thread in PHP Forum Timeline: editing





About Us | Contact Us | Advertise | Acceptable Use Policy
Forum Index | Build Custom RSS Feed


Follow us on Twitter


© 2011 DaniWeb® LLC