Search code examples
phpcookiesgetechourlvariables

2nd Variable in url in echoing


Ok, I am trying to get a variable from a url using PHP. Currently the site I am working with gets a the first url and sets it as a cookie. I am trying to get the second url and use it with our analytics software. The current url is: http://www.peeweepenguin.com?cu=10031 the url I am trying to use is http://www.peeweepenguin.com?cu=10031&MyID=SomeCompany

When putting the link like this http://www.peeweepenguin.com?MyID=SomeCompany I can get the url to echo out. However I cannot get it to work when echoing it out with the url above (http://www.peeweepenguin.com?cu=10031&MyID=SomeCompany)

Here is the code I am working with:

    <?php 
    if (isset($_GET['MyID']));
    $currentCUSite = $_GET['MyID'];
    ?>

Code above is in head tag.

    <?php echo $currentCUSite; ?>

Code above is located in body.


Solution

  • Your if statement isn't working properly.

    if (isset($_GET['MyID']));
                             ^
    

    That semicolon terminates your if statement and the statement below it will get executed every time.

    Change your code to:

    if (isset($_GET['MyID'])) {
        $currentCUSite = $_GET['MyID'];
    }
    

    Or:

    $currentCUSite = (isset($_GET['MyID'])) ? $currentCUSite : '';
    

    And in your body, you can do:

    <?php if(isset($currentCUSite)) echo $currentCUSite; ?>