Go Back   CodingForums.com > :: Server side development > PHP

Before you post, read our: Rules & Posting Guidelines

Reply
 
Thread Tools Rate Thread
Enjoy an ad free experience by logging in. Not a member yet? Register.
Old 04-05-2004, 12:06 PM   PM User | #1
PenguinJr
New Coder

 
Join Date: Feb 2004
Posts: 17
Thanks: 0
Thanked 0 Times in 0 Posts
PenguinJr is an unknown quantity at this point
Manipulating URL

Hi guys,

Lets say i've passed a URL of www.wateva.com/test.php?user=$user from a php page that contained the variable $user with these codes.
Code:
$user = $_POST['username'];
header ("location: test.php?user=$user");
so when they're taken to the test page they should see www.wateva.com/test.php?user=Bob
for example.

Now...I wanna keep passing the variable of user and when I need it I want to be able to use that variable.

1st question: How do i pass the variable around in the URL from page to page?
2nd question: How do i use it later on, as in how do i assign it to a variable?

Thank you.
PenguinJr is offline   Reply With Quote
Old 04-05-2004, 12:14 PM   PM User | #2
missing-score
Senior Coder


 
missing-score's Avatar
 
Join Date: Jan 2003
Location: UK
Posts: 2,194
Thanks: 0
Thanked 0 Times in 0 Posts
missing-score is on a distinguished road
im not sure what you mean, do you want to pass a variable name and have that variable set.

eg:

PHP Code:
// url: /file.php?user=$user

$user 'Bob';
echo 
$_GET['user']; // should echo 'Bob'; 
If thats what you want to do, there are different ways you can go about this. You could use eval(), however that could be very risky. If you just want the one variable, I would use a basic str_replace and replace the value. The only thing I wonder, is if have user set, you could just check if it is set and if it is set $user = username.

If thats not what you want to do, please could you explain a little more.
missing-score is offline   Reply With Quote
Old 04-05-2004, 12:30 PM   PM User | #3
raf
Master Coder


 
Join Date: Jul 2002
Posts: 6,589
Thanks: 0
Thanked 0 Times in 0 Posts
raf will become famous soon enoughraf will become famous soon enough
Quote:
Originally Posted by PenguinJr
1st question: How do i pass the variable around in the URL from page to page?
2nd question: How do i use it later on, as in how do i assign it to a variable?
Thank you.
Euh. Exactly like the code you posted, but with $_GET instead of $_POST. And then you need it to append to each link on your page ...

But i would realy recommend storing it inside a sessionvariable.

like
PHP Code:
session_start();  // at the top of your script
...
$_SESSION['uservar'] = $_GET['user']; 
to set the sessionvar, and
PHP Code:
session_start();  // at the top of your script
...
$user $_SESSION['uservar']; 
to get the value back.
__________________
Posting guidelines I use to see if I will spend time to answer your question : http://www.catb.org/~esr/faqs/smart-questions.html
raf is offline   Reply With Quote
Old 04-05-2004, 12:33 PM   PM User | #4
missing-score
Senior Coder


 
missing-score's Avatar
 
Join Date: Jan 2003
Location: UK
Posts: 2,194
Thanks: 0
Thanked 0 Times in 0 Posts
missing-score is on a distinguished road
[ot] *Smacks self round head for not remembering sessions* [/ot]
missing-score is offline   Reply With Quote
Reply

Bookmarks

Jump To Top of Thread


Thread Tools
Rate This Thread
Rate This Thread:

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 On
HTML code is Off

Forum Jump


All times are GMT +1. The time now is 05:21 PM.


Advertisement
Log in to turn off these ads.