1. This site uses cookies. By continuing to use this site, you are agreeing to our use of cookies. Learn More.

Little PHP help

Discussion in 'PHP & Perl' started by Butler, Aug 24, 2008.

  1. Butler

    Butler Registered Member Premium Member

    Joined:
    Feb 4, 2008
    Messages:
    71
    Likes Received:
    1
    Just started learning php and it's not going well. I am trying to get this code to work

    PHP:
    <?php
        $referer 
    $_SERVER['HTTP_REFERER'];
        if(
    $referer == "")
        {
            echo 
    "hxxp://www.mydomain.com/trackingscript.php"
    else
            echo 
    "<img src="hxxp://www.mydomain.com/image.jpg">";   
        
    }
    ?>
    But it keeps giving me errors? Can anyone let me know what I have done wrong, I have tried a few things but nothing is working for me.

    Also, what is the best site to learn php from scratch?
     
  2. catman08

    catman08 Junior Member

    Joined:
    Jan 11, 2008
    Messages:
    171
    Likes Received:
    109
    Occupation:
    IM
    Location:
    Europe
    Hi try that:

    PHP:

    <?php
        $referer 
    $_SERVER['HTTP_REFERER'];
        if(
    $referer == "")
        {
            echo 
    'hxxp://www.mydomain.com/trackingscript.php'
        }
    else
       {
            echo 
    '<img src="hxxp://www.mydomain.com/image.jpg">';   
        }
    ?>


    ... your problem were the quotes used 2 times behind each other ... better use ' " " ' or " ' ' " --> see the use of single quote ;-)
    And you forgot to close and reopen the bracket { before and after the else

    cheers catman

    P.S. Do not forget to change the urls though --> also note to repace the hxxp with http ;-)
     
    • Thanks Thanks x 1
    Last edited: Aug 24, 2008
  3. soulchief

    soulchief Junior Member

    Joined:
    Oct 17, 2007
    Messages:
    117
    Likes Received:
    55
    Location:
    Canada
    best way to learn php is reading a few tutorials and looking at the code of free php scripts.
     
    • Thanks Thanks x 1
  4. Butler

    Butler Registered Member Premium Member

    Joined:
    Feb 4, 2008
    Messages:
    71
    Likes Received:
    1
    Ah, thanks, it's the small things like this I need to pick up on.

    Yeah, I was just using the xx's cause it's become a habit when posting on this forum.

    Is there any way I can include this code in the echo ' ' part

    header( 'Location: http://www.mydomain.com/new_page.html' ) ;

    So it loads the requested page instead of showing a link?
     
  5. soulchief

    soulchief Junior Member

    Joined:
    Oct 17, 2007
    Messages:
    117
    Likes Received:
    55
    Location:
    Canada
  6. Butler

    Butler Registered Member Premium Member

    Joined:
    Feb 4, 2008
    Messages:
    71
    Likes Received:
    1
    I was trying to put it in the '' after the echo, am such a dumb ass.

    Thanks for the help guys, off to do some learning.
     
  7. catman08

    catman08 Junior Member

    Joined:
    Jan 11, 2008
    Messages:
    171
    Likes Received:
    109
    Occupation:
    IM
    Location:
    Europe
    Hey Butler ...

    ... am glad we could help you.

    One page that i found is very usefull for the students that i teach in php is the page hxxp://tiz*ag.c0m

    there you have a crash course inclusive easy to understand tutorials for several programming languages (like php, javascrip, aspnet, mysql, sql, ajax, ...)

    I think this will help you a lot.

    Many Greetings
    Catman

    P.S. I know thwe beggining of learning the first programming language is sometimes hard sometimes even frustrating :pcguru: ... but the time where you will have a clear understanding of everthing will come fast. Then programming is a lot of fun and learning new languages has become a lot easier too. :)
     
    • Thanks Thanks x 1
    Last edited: Aug 24, 2008
  8. Essential Clix

    Essential Clix Executive VIP Premium Member

    Joined:
    Jul 30, 2007
    Messages:
    1,755
    Likes Received:
    2,791
    Location:
    USA
    You might want to check out the PHP section of this forum, which is where I'm moving this thread now...
     
  9. barigain

    barigain Junior Member

    Joined:
    Aug 23, 2012
    Messages:
    100
    Likes Received:
    12
    There is no reason to avoid HTTP_REFERER. Just when using it, be aware that it's not always set, and that it can be freely manipulated by the client, so it is untrusted data.
    The vast majority of clients sets the variable, and does so correctly.
    The main reason for blocking it is privacy: For example, when opening an E-Mail in a web mail client, links to external images would carry the web mail service's address in the HTTP_REFERER header. That's why GMail and Yahoo make efforts to block it.
    The workaround you suggest works only for movements within the same site, and will break if the user has more than one tab/browser window open with which they browse your site. If you need to know the referring page, the superior method is to add a GET parameter like
    <a href="otherpage.html?from=thispage.html">
    One example where relying on HTTP_REFERER is dangerous would be showing a live-updated list of referring sites on your main page ("Visitors came from...") it would be easy to smuggle arbitrary URLs into that list by visiting your site with a fake HTTP_REFERER set.
     
  10. member8200

    member8200 Regular Member

    Joined:
    Aug 9, 2014
    Messages:
    469
    Likes Received:
    33

    <?php

    //either this one
    $referer
    = $_SERVER['HTTP_REFERER'];
    if(
    $referer == "")
    {
    echo
    "http://www.mydomain.com/trackingscript.php";
    else {
    echo
    "<img src='
    http://www.mydomain.com/image.jpg'>";
    }
    ?>



    <?php
    //or this one
    $referer
    = $_SERVER['HTTP_REFERER'];
    if(
    $referer == "")
    {
    echo
    'http://www.mydomain.com/trackingscript.php';
    else {
    echo
    '<img src="
    http://www.mydomain.com/image.jpg">';
    }
    ?>






    Please take a review about using '' and "" in php. It's one of the most important thing to consider specially in working with php variables.

    Cheers mate! :)