Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 4 of 4
  1. #1
    New Coder
    Join Date
    Feb 2004
    Posts
    17
    Thanks
    0
    Thanked 0 Times in 0 Posts

    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.

  • #2
    Senior Coder missing-score's Avatar
    Join Date
    Jan 2003
    Location
    UK
    Posts
    2,194
    Thanks
    0
    Thanked 0 Times in 0 Posts
    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.

  • #3
    raf
    raf is offline
    Master Coder
    Join Date
    Jul 2002
    Posts
    6,589
    Thanks
    0
    Thanked 0 Times in 0 Posts
    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

  • #4
    Senior Coder missing-score's Avatar
    Join Date
    Jan 2003
    Location
    UK
    Posts
    2,194
    Thanks
    0
    Thanked 0 Times in 0 Posts
    [ot] *Smacks self round head for not remembering sessions* [/ot]


  •  

    Posting Permissions

    • You may not post new threads
    • You may not post replies
    • You may not post attachments
    • You may not edit your posts
    •