SitePoint Sponsor

User Tag List

Results 1 to 24 of 24
  1. #1
    SitePoint Enthusiast
    Join Date
    May 2003
    Location
    Stamford, CT, USA
    Posts
    99
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    Smile Call PHP from javascript?

    Hi,

    I'm new to PHP and may be off base in what I'm attempting.

    Here's the scenario. I wrote some PHP code to record a site visit to a file. The idea is that each time a user visits a page, I create a record with that user's ip, domain, referrer and the name of the page visited.

    The code works fine BUT in order to implement it I would have to insert the code in every page and I'd have to rename each page to *.PHP instead of *.HTML.

    What I'm thinking is that if I could put a short javascript function on each page that function could call the PHP code. The javascript would somehow have to pass the name of the page back to the PHP code.

    Am I off base here? Maybe there's a better way to do this using PHP only?

    Thanks for your help,

    Ed

  2. #2
    SitePoint Wizard siteguru's Avatar
    Join Date
    Oct 2002
    Location
    Scotland
    Posts
    3,631
    Mentioned
    2 Post(s)
    Tagged
    0 Thread(s)
    JavaScript runs on the client, so the only way to do this would be for JavaScript to pop open a small window with your PHP file in it, that file would do the taks you wish, and then it would auto-close the popup.

    The complexity of doing this suggests to me that you would be better renaming all your .HTM files to .PHP and edit then to include() the PHP functions.
    Ian Anderson
    www.siteguru.co.uk

  3. #3
    SitePoint Enthusiast
    Join Date
    May 2003
    Location
    Stamford, CT, USA
    Posts
    99
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Quote Originally Posted by siteguru
    The complexity of doing this suggests to me that you would be better renaming all your .HTM files to .PHP and edit then to include() the PHP functions.
    Hi,

    Thanks for your help.

    The reason I'm reluctant to rename all the htm files to php is that means that ever page will have to be completely parsed at the server when only a small portion of the page would actually be php code. It seems like an un-necessary drain on server resources.

    Couldn't this be done with some sort of "invisible" submit from the javascript? The submit would have to be done at page load time w/o requiring the user to click on anything.


    Thanks, Ed
    Last edited by Ed Hunter; May 13, 2003 at 08:37.

  4. #4
    "Of" != "Have" bronze trophy Jeff Lange's Avatar
    Join Date
    Jan 2003
    Location
    Calgary, Canada
    Posts
    2,063
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    you could write a PHP script which serves an image, and then use
    Code:
    <img src="recordingscript.php" alt="" />
    to access it.

    Or a
    Code:
    <link rel="stylesheet" type="text/css" href="recordingscript.php" />
    Who walks the stairs without a care
    It shoots so high in the sky.
    Bounce up and down just like a clown.
    Everyone knows its Slinky.

  5. #5
    SitePoint Enthusiast
    Join Date
    May 2003
    Location
    Stamford, CT, USA
    Posts
    99
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Hi cyborg,

    That looks promising.

    Does the php script actually have to return an image or is that just a "pretext" to get the page to call the php code?

    Is there some way to pass the name of the page to the php using your ideas?


    Thanks, Ed

  6. #6
    "Of" != "Have" bronze trophy Jeff Lange's Avatar
    Join Date
    Jan 2003
    Location
    Calgary, Canada
    Posts
    2,063
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    yes, in the PHP script you'd need to do a
    PHP Code:
    readfile('someimage.jpg'); 
    to return the correct image.

    you may be able to use $_SERVER['HTTP_REFERER'] to find which page it is being called from, but that's not for sure.
    Who walks the stairs without a care
    It shoots so high in the sky.
    Bounce up and down just like a clown.
    Everyone knows its Slinky.

  7. #7
    SitePoint Enthusiast
    Join Date
    May 2003
    Location
    Stamford, CT, USA
    Posts
    99
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Quote Originally Posted by cyborg from dh
    yes, in the PHP script you'd need to do a
    PHP Code:
    readfile('someimage.jpg'); 
    to return the correct image.
    Hi,

    I'm having trouble getting this to work.

    In the html I have this:


    Code:
    <html>
    <head>
      <title></title>
    </head>
    <body>
    <img src="logtest.php" alt="" />
    </body>
    </html>
    In logtest.php I have:

    Code:
    <?php readfile('transparent.gif'); ?>
    But all I get from the html is an "x in a box".

    If I run the php directly I get a bunch of garbage which is the actual contents of transparent.gif.

    Any idea what's wrong?

    Thanks, Ed

    P.S. How do you get your code example to show up in a box?

    Thanks, Ed
    Last edited by Ed Hunter; May 16, 2003 at 20:12.

  8. #8
    ********* Wizard silver trophy Cam's Avatar
    Join Date
    Aug 2002
    Location
    Burpengary, Australia
    Posts
    4,495
    Mentioned
    0 Post(s)
    Tagged
    1 Thread(s)
    Quote Originally Posted by Ed Hunter
    P.S. How do you get your code example to show up in a box?
    Use vB Code, here is a quick tutorial.

  9. #9
    SitePoint Enthusiast
    Join Date
    May 2003
    Location
    Stamford, CT, USA
    Posts
    99
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Quote Originally Posted by DJ P@CkMaN
    Use vB Code, here is a quick tutorial.
    Thanks! That was a big help.

    Ed

  10. #10
    "Of" != "Have" bronze trophy Jeff Lange's Avatar
    Join Date
    Jan 2003
    Location
    Calgary, Canada
    Posts
    2,063
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Do you have any line returns before or after the <?php or ?>...
    Who walks the stairs without a care
    It shoots so high in the sky.
    Bounce up and down just like a clown.
    Everyone knows its Slinky.

  11. #11
    SitePoint Enthusiast
    Join Date
    May 2003
    Location
    Stamford, CT, USA
    Posts
    99
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Quote Originally Posted by cyborg from dh
    Do you have any line returns before or after the <?php or ?>...
    No. I posted the entire file.
    The "real" php has other directives but for this test I striped everything out except what I posted.


    Thanks, Ed

  12. #12
    We like music. weirdbeardmt's Avatar
    Join Date
    May 2001
    Location
    Channel Islands Girth: Footlong
    Posts
    5,882
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Hi, I'm not sure if this is exactly what you want, but you could use .htaccess to parse .html files as PHP. It wouldn't cut the server drain you mentioned, but it'd mean you don't have to rename all your files.

    PHP Code:
    AddType application/x-httpd-php .html 
    HTH.
    I swear to drunk I'm not God.
    Matt's debating is not a crime
    Hint: Don't buy a stupid dwarf Clicky

  13. #13
    SitePoint Enthusiast
    Join Date
    May 2003
    Location
    Stamford, CT, USA
    Posts
    99
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    Smile

    Quote Originally Posted by weirdbeardmt
    Hi, I'm not sure if this is exactly what you want, but you could use .htaccess to parse .html files as PHP. It wouldn't cut the server drain you mentioned, but it'd mean you don't have to rename all your files.

    PHP Code:
    AddType application/x-httpd-php .html 
    HTH.
    Thanks for the info. That would be my last resort.

    I'm still wondering why
    Code:
    <?php readfile('transparent.gif'); ?>
    doesn't work. It feels as though I'm so close. If I could only find the key to make it work.

    Is it a lost cause?

    Thanks, Ed

  14. #14
    "Of" != "Have" bronze trophy Jeff Lange's Avatar
    Join Date
    Jan 2003
    Location
    Calgary, Canada
    Posts
    2,063
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    I forgot to mention: you need to send a header for the file type.

    PHP Code:
    <?php
    header
    ('Content-Type: image/gif');
    readfile('transparent.gif');
    ?>
    (If you use a JPEG, you'd use image/jpeg, PNG, image/png).
    Who walks the stairs without a care
    It shoots so high in the sky.
    Bounce up and down just like a clown.
    Everyone knows its Slinky.

  15. #15
    SitePoint Enthusiast
    Join Date
    May 2003
    Location
    Stamford, CT, USA
    Posts
    99
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    Talking

    Quote Originally Posted by cyborg from dh
    I forgot to mention: you need to send a header for the file type.

    PHP Code:
    <?php
    header
    ('Content-Type: image/gif');
    readfile('transparent.gif');
    ?>
    (If you use a JPEG, you'd use image/jpeg, PNG, image/png).

    Works like a charm!

    That's a hugh help!

    Many thanks,

    Ed

    P.S. I tried to add to your rep but I had already done that and it would not let me add more.

    Ed

  16. #16
    SitePoint Enthusiast
    Join Date
    May 2003
    Location
    Stamford, CT, USA
    Posts
    99
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    OK, Now that that's resolved, the next step is to pass a variable to the php.

    I tried this.

    In the calling html:

    Code:
    <img src="logtest.php?name=home" alt="" />
    And in logtest.php:

    Code:
    logToFile("log.txt", $_GET["name"]);
    Nothing is being written to the log file.

    LogToFile is a function that works.

    Any ideas?

    Thanks, Ed

  17. #17
    Non-Member
    Join Date
    Jan 2003
    Posts
    5,748
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    JavaScript runs on the client, so the only way to do this would be for JavaScript to pop open a small window with your PHP file in it, that file would do the taks you wish, and then it would auto-close the popup.

    The complexity of doing this suggests to me that you would be better renaming all your .HTM files to .PHP and edit then to include() the PHP functions.
    I do not think so mate I use Javascript/PHP a lot of the time to allow clients to communicate with server-side scripting all the time.

    It is very easy to implement, easy to administer and very portable. Below is a sample template I work from:

    PHP Code:
     /* database driven content feed using PHP and Javascript */
     /*
     step 1: include following in seperate PHP file.
     step 2: place following Javascript tag below in BODY of document as required.
     
      <script language="javascript1.2" type="text/javascript" src="*.php"></script>
     
     step 3: file required to grab content from database and display to basic HTML page.
     */
     
     <?
     
    include_once("../classes.php");
     
     
    $db = new db_connect;
     
    $db -> select_user_db();
     
     if(!
    $db) { ?> document.write("Cound not load content feed."); <? }
     
     
    $sql $db -> query_user_db("SELECT ** FIELDS TO USE ** FROM ** TABLE TO USE ** ORDER BY ** PRIMARY KEY **");
     
    ?>
     document.write("<table border='0' width='512' align='center' valign='top' cellspacing='2' cellpadding='0'><tbody>");
     <?
     
    while($row mysql_fetch_array($sql))
     {
      
    $ID $row["** ID **"];
      
    $TITLE $row["** TITLE **"];
      
    ?>
      document.write("<tr><td align='left' width='100%' height='24' valign='center'>&nbsp;");
      document.write("<a href="*.php?id=<? echo($ID); ?>" target='_blank'>");
      document.write("<font size='2' face='verdana' color='black'><? echo($TITLE); ?></font>");
      document.write("</a></td></tr>");
      <?
     
    }
     
    ?>
     document.write("</tbody></table>");
    You then include a Javascript link within the webpage you need the PHP script to work from. In my example above, I have a TABLE structured to hold elements from the database; though you can execute any script you want from PHP, in this case to dump/get user's IP, Domain address etc.

    No problem at all.... very convienent as well. Here is a sample link:

    PHP Code:
    <script language="javascript1.2" type="text/javascript" src="YourFileOn YourServer.php"></script> 
    Even though the file extension is PHP, the browser/Javascript will still execute the javascript side of things - it doesn't matter;

    You need to use the PHP extension so the PHP script part of the above file will be parsed by your server.

    This'll sort out your reluctant need to rename all your webpages extensions.

  18. #18
    SitePoint Zealot
    Join Date
    Mar 2002
    Location
    Perth, Australia
    Posts
    157
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Quote Originally Posted by Ed Hunter
    I tried this.

    In the calling html:
    Code:
    <img src="logtest.php?name=home" alt="" />
    And in logtest.php:
    Code:
    logToFile("log.txt", $_GET["name"]);
    Nothing is being written to the log file.
    Being an image, it could be getting cached by your browser (especially IE) - have you tried emptying your cache? You could also try sending some no-cache and expiry headers to prevent the image getting cached.

    Here are some headers (but only use them if caching is a problem for you):

    PHP Code:
    <?php 
    header
    ("Expires: Mon, 26 Jul 1997 05:00:00 GMT");    // Date in the past
    header("Last-Modified: " gmdate("D, d M Y H:i:s") . " GMT"); 
                                                         
    // always modified
    header("Cache-Control: no-store, no-cache, must-revalidate");  // HTTP/1.1
    header("Cache-Control: post-check=0, pre-check=0"false);
    header("Pragma: no-cache");                          // HTTP/1.0
    ?>
    Paul Davey
    webmaster for Whitford Church of Christ

  19. #19
    SitePoint Evangelist Mr. Brownstone's Avatar
    Join Date
    May 2002
    Location
    Relative
    Posts
    452
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Of course, that's just my opinion. I could be wrong.

  20. #20
    SitePoint Zealot Ghandi's Avatar
    Join Date
    Sep 2002
    Location
    Kansas
    Posts
    118
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    I am in the thinking that if HTML isn't in the <?php and ?> then it isn't parsed by PHP.

    If that is the case then you could do the .htaccess and include your code in every file and the only server drain would be the task you are performing.

    If you were to go to every file and include that code you might as well rename it all to .php IMO.


    Now if I am wrong you could have a very tiny small iframe which you could have pretty much invisibile and run your php scripts through it. Just another possibility
    W.W.G.D. :: What Would Gandhi Do
    http://patkohler.blogspot.com/

  21. #21
    SitePoint Zealot
    Join Date
    Mar 2002
    Location
    Perth, Australia
    Posts
    157
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Quote Originally Posted by Ghandi
    If you were to go to every file and include that code you might as well rename it all to .php IMO.
    But that would require updating all the links in the site.

    Quote Originally Posted by Ghandi
    Now if I am wrong you could have a very tiny small iframe which you could have pretty much invisibile and run your php scripts through it. Just another possibility
    iframes are IE only - it wouldn't work with other browsers?
    Paul Davey
    webmaster for Whitford Church of Christ

  22. #22
    Non-Member
    Join Date
    Jan 2003
    Posts
    5,748
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    IFRAMES are cross browser though all have various states of functionality;

    But why bother ? The methods I've described would be the ideal solution and that folks is an unbiased view.

    After all - you need a problem solved correct ?

  23. #23
    SitePoint Enthusiast
    Join Date
    May 2003
    Location
    Stamford, CT, USA
    Posts
    99
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    Cool

    It's working!!!

    I don't know why it didn't work before; but I made some minor changes and it works!

    This is an exceptionally simple way to pass data from a client back to a php script on the server.

    It should, I think, place a very light load on the server.

    Many thanks to "cyborg from dh" for this.

    Just to recap, in case anyone needs it. In the HTML include this:

    Code:
    <img src="whatever.php?<variable name>=<data-to-pass" alt="" />
    In whatever.php, include the following:
    PHP Code:
    header('Content-Type: image/gif');
    readfile('transparent.gif');
    //do anything you like using $_GET["<variable name>"] when needed 
    I used a transparent gif for now because I don't really need to return an image to the HTML page.

    I'm sure you can pass more than one variable as needed.

    Ed

  24. #24
    "Of" != "Have" bronze trophy Jeff Lange's Avatar
    Join Date
    Jan 2003
    Location
    Calgary, Canada
    Posts
    2,063
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    No problem, glad to help.
    Who walks the stairs without a care
    It shoots so high in the sky.
    Bounce up and down just like a clown.
    Everyone knows its Slinky.


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
  •