SitePoint Sponsor

User Tag List

Results 1 to 4 of 4
  1. #1
    SitePoint Guru
    Join Date
    Mar 2001
    Location
    Highlands Ranch, Colorado
    Posts
    614
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    Embedding variable into URL

    I am having problems taking a value assigned to a variable and to include it into a URL.

    Here is what I am doing.

    Page #1 = value assigned to variable. Visitor clicks on a link. Variable is passed onto the page #2. Page #2 has a meta refresh URL and needs the value of the variable embedded into the link. If I echo out the value it shows up fine - so, I know it is carried over properly.

    Here is my code that shows the URL and how I am trying to add the value to my URL. Any help would be appreciated.

    PHP Code:
    <?php
    //start session to catch the session variable from page 1
    session_start();
    $session_data session_encode();
    $v1 $_SESSION['data2'];
    ?>
    <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
    <html xmlns=" http://www.w3.org/1999/xhtml">
    <head>
    <meta http-equiv="Refresh" content="0;URL=http://mydomain.com?ad=12345&s1=<? echo $v1?>">
    <title>My Page title</title>
    </head>
    <body>
    <? echo $v1?>
    </body>
    </html>
    I have tried several different things to get that value added to the URL. Nothing worked.

    Thanks.

    Christoph

  2. #2
    SitePoint Wizard bronze trophy
    Join Date
    Jul 2006
    Location
    Augusta, Georgia, United States
    Posts
    4,147
    Mentioned
    16 Post(s)
    Tagged
    3 Thread(s)
    <?php ?> rather than <? ?> may solve the problem. You may also want to consider some type of default value in case the value isn't passed. I would recommend using isset do this rather than just assuming its going to be there. That is generally bad practice. You should always assume the worst and be ready to handle it.

  3. #3
    SitePoint Guru
    Join Date
    Mar 2001
    Location
    Highlands Ranch, Colorado
    Posts
    614
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Quote Originally Posted by oddz View Post
    <?php ?> rather than <? ?> may solve the problem. You may also want to consider some type of default value in case the value isn't passed. I would recommend using isset do this rather than just assuming its going to be there. That is generally bad practice. You should always assume the worst and be ready to handle it.
    Thanks. <?php did the trick. Strange, but ...
    Good hint with the default value. I will add that.

    Thanks for the quick help

    Christoph

  4. #4
    SitePoint Wizard bronze trophy
    Join Date
    Jul 2006
    Location
    Augusta, Georgia, United States
    Posts
    4,147
    Mentioned
    16 Post(s)
    Tagged
    3 Thread(s)
    A simple ternary is normally how I handle these situations.

    $v1 = isset($_SESSION['data2'])?$_SESSION['data2']:'default';


Bookmarks

Posting Permissions

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