Randombanner.php Works - Include... Doesn't

Discussion in 'PHP' started by jakeruston, Dec 16, 2007.

  1. #1
    Hi,

    I'm trying to make a quick and easy Ad Rotation Script. I made a file, randombanner.php, and it goes as follows:

    
    <?php
    $number=rand(0, 100);
    
    if ($number < 50)
    {
    echo '<script type="text/javascript"><!--
    google_ad_client = "pub-3873295902304841";
    //728x90, created 16/12/07
    google_ad_slot = "2029938138";
    google_ad_width = 728;
    google_ad_height = 90;
    //--></script>
    <script type="text/javascript"
    src="http://pagead2.googlesyndication.com/pagead/show_ads.js">
    </script>';
    }
    else
    {
    echo '<!-- START CUSTOM WIDGETBUCKS CODE --><div><script src="http://www.widgetbucks.com/script/ads.js?uid=y4cEXhlQhFGjnIVq"></script></div><!-- END CUSTOM WIDGETBUCKS CODE -->';
    }
    
    ?>
    
    Code (markup):
    When I go to the URL, a random banner is displayed just fine.

    However, it becomes a problem when I try to include it into the document.

    I use an include, like this:

    
    <?php include 'http://www.XXXX.XXX/randombanner.php';?>
    
    Code (markup):
    Nothing is shown in the document when I do it this way.

    How to fix?

    Thanks,
    Jake.
     
    jakeruston, Dec 16, 2007 IP
    hogan_h likes this.
  2. Colbyt

    Colbyt Notable Member

    Messages:
    3,224
    Likes Received:
    185
    Best Answers:
    0
    Trophy Points:
    210
    #2
    Try:

    <?php include (randombanner.php); ?>

    Note the space between the ; and ? is intentional.

    edit: I think you still have a potential problem with your conditional statement when the random number is 50. <= might be a better choice.
     
    Colbyt, Dec 16, 2007 IP
  3. hogan_h

    hogan_h Peon

    Messages:
    199
    Likes Received:
    30
    Best Answers:
    0
    Trophy Points:
    0
    #3
    Your main problem is probably that you are using http path to include the file.
    Some servers don't support it and it can cause problems, read here: http://www.php.net/manual/en/function.include.php

    Your safest bet is to use local or absolute path to the file, ie:
    <?php include ("randombanner.php"); ?>
    or
    <?php include ("/www/home/usersite/public_html/banners/randombanner.php"); ?>
     
    hogan_h, Dec 16, 2007 IP
  4. Colbyt

    Colbyt Notable Member

    Messages:
    3,224
    Likes Received:
    185
    Best Answers:
    0
    Trophy Points:
    210
    #4
    hogan is right. I removed your single quotes and failed to put the doubles back in.
     
    Colbyt, Dec 16, 2007 IP