1. Advertising
    y u no do it?

    Advertising (learn more)

    Advertise virtually anything here, with CPM banner ads, CPM email ads and CPC contextual links. You can target relevant areas of the site and show ads based on geographical location of the user if you wish.

    Starts at just $1 per CPM or $0.10 per CPC.

<script> tag for php files

Discussion in 'PHP' started by ravianz, Oct 30, 2005.

  1. #1
    Hi!
    as we can use a javascript using
    <script src="xyz.js"></script> tag in .htm file.
    can we use such a script for a php file?
    infect i want to use a php counter on htm page but want to avoid <iframe> tag?
    in the php file after some commands one variable is displayed like
    echo $count;
    i've tried using
    <script src="http://domin.ext/counter.php"></script>
    but its not showing the variable value though counter works in this way i.e. counter is incremented but not displayed on .htm page;
    I have also the tried script tag for a php file having a single echo statement for variable as well for string; also i have tried image on php file bt no display;
    please help me out if it is possible to display php file in this way...
    SEMrush
     
    ravianz, Oct 30, 2005 IP
    SEMrush
  2. nevetS

    nevetS Evolving Dragon

    Messages:
    2,544
    Likes Received:
    211
    Best Answers:
    0
    Trophy Points:
    135
    #2
    You cannot include a php file from within a script tag. PHP is a server side scripting language, and the <script> tag denotes a client side script.

    <iframe> will essentially include an external file so that's really your only option I would think -- aside from parsing all of your htm files as php and doing something like <? require_once('/path/to/counter.php' ?>

    You can parse htm files as php from within a .htaccess file. Syntax may be off, but it's something like:
    ForceType application/php .htm

    You can google around or search these forums for the exact syntax. There are a lot of references.
     
    nevetS, Oct 30, 2005 IP
    ravianz likes this.
  3. smo

    smo Well-Known Member

    Messages:
    41
    Likes Received:
    1
    Best Answers:
    0
    Trophy Points:
    123
    #3
    You can include a php file using javascript src tag. You have to take care of header tag inside the php file. I have developed on counter script which works in any html page. Through JavaScript the php codes get execuated.

    This is the code. If you use this you will get a counter at any html or any other page.

    <script language="javascript" src="http://www.plus2net.com/counter/count.php?id=3150311751"> </script>

    The header of the count.php files says
    Header("content-type: application/x-javascript");

    Try with this ....
    HTH
     
    smo, Oct 31, 2005 IP
  4. clickbuild

    clickbuild Member

    Messages:
    89
    Likes Received:
    4
    Best Answers:
    0
    Trophy Points:
    48
    #4
    Actually you can. Here is the code from the adsense tracker I use on my site:
    <script language="javascript" src="/astracker/ast.php"></script>
    Code (markup):
    It's the action of requesting the script that makes it execute server side - if you return javascript then that code will execute in the browser.

    Call the PHP file in your browser and ensure it is working, then throw it into the javascript request and use "Live HTTP Headers" plugin for firefox to ensure it is being requested correctly and you should be good to go...

    Now that I think of it, the NicheBot Adsense Tracker also uses that trick....


    Hope that helps.

    Steve.

    PS. the ForceType trick is also cool, just put the htaccess file in a directory and drop a a php file in there, but make the extension .js - it will return like a js file but be parsed via PHP
     
    clickbuild, Oct 31, 2005 IP
  5. clickbuild

    clickbuild Member

    Messages:
    89
    Likes Received:
    4
    Best Answers:
    0
    Trophy Points:
    48
    #5
    Now that I am re-reading your first post - I see I did not quite answer the question...

    If your PHP code is executing fine and doing the increment as expected, the problem is that you are not returning javascript for the browser to render...

    try returning the count in a properly formatted document.write statement
     
    clickbuild, Oct 31, 2005 IP
  6. ravianz

    ravianz Well-Known Member

    Messages:
    1,538
    Likes Received:
    56
    Best Answers:
    0
    Trophy Points:
    195
    #6

    Still facing the same problem. Now i am testing it such that i have counter.php placed in C:\apache\htdocs\test\counter.php
    and content of this file is:

    
    <?php 
    header("content-type: application/x-javascript"); 
    ?>
    <?php 
    echo '<script type="text/javascript">';
    echo "var count = 32;";
    echo "document.write (count); ";
    	echo "</script>";
    ?>
    PHP:
    i have also tried the following code as well

    <?php 
    header("content-type: application/x-javascript"); 
    ?>
    <?php 
    $count  = 100;
    echo $count;
    ?>
    
    PHP:

    and in the test.htm file placed on my desktop i am using:

    <script language="javascript" src="http://localhost/test/counter.php"></script>


    but the values are not displayed in htm file. And when the counter.php is run just by typing http://localhost/test/counter.php
    following error is produced:

    Warning: Cannot add header information - headers already sent by (output started at c:\apache\htdocs\test\counter.php:2) in
    c:\apache\htdocs\test\counter.php on line 3

    Again when trying this with actual counter code file counter is working properly but not displayed in htm file;

    Please suggest any solution;
     
    ravianz, Oct 31, 2005 IP
  7. nevetS

    nevetS Evolving Dragon

    Messages:
    2,544
    Likes Received:
    211
    Best Answers:
    0
    Trophy Points:
    135
    #7
    Headers have to be the first thing sent. Strangely it looks like your headers are the first line. I'd check the file again to make sure.
     
    nevetS, Oct 31, 2005 IP
  8. ravianz

    ravianz Well-Known Member

    Messages:
    1,538
    Likes Received:
    56
    Best Answers:
    0
    Trophy Points:
    195
    #8

    headers are in the first line
     
    ravianz, Oct 31, 2005 IP
  9. durango

    durango Guest

    Messages:
    83
    Likes Received:
    7
    Best Answers:
    0
    Trophy Points:
    0
    #9
    Try the following, as you are already telling the browser what you are executing is Javascript you do not need <script> tags in your PHP. The <script src="/myscript.php" type="text/javascript"></script> tells the browser that whatever code is in myscript.php is valid javascript.

    
    <?php 
    header("content-type: application/x-javascript"); 
    echo "var count = 32;";
    echo "document.write (count); ";
    ?>
    PHP:
    [edit: I also have done this many many times without the need to include a header. Simply output the javascript code]
     
    durango, Nov 1, 2005 IP
  10. ravianz

    ravianz Well-Known Member

    Messages:
    1,538
    Likes Received:
    56
    Best Answers:
    0
    Trophy Points:
    195
    #10


    Thanks A Lot; thats surely working properly ; Thanks to all of you;
     
    ravianz, Nov 1, 2005 IP