开发者

Running php script after page loaded [closed]

Closed. This question is off-topic. It is not currently accepting answers.

Want to improve this question? Update the question so it's on-topic for Stack Overflow.

开发者_如何学运维

Closed 11 years ago.

Improve this question

Everytime a user visits a wordpress single-post page, the script update_comments.php is executed. It works the way it should, but the user won't see the wordpress post, or anything for that matter, until the script has finished executing. I simply want to initiate the script without the user noticing a single thing. ignore_user_abort(true) is on in update_comments.php.

<?php
global $post;
$theid= $post->ID;
$url = (!empty($_SERVER['HTTPS'])) ? "https://".$_SERVER['SERVER_NAME'].$_SERVER['REQUEST_URI'] : "http://".$_SERVER['SERVER_NAME'].$_SERVER['REQUEST_URI'];
preg_match('/http(.*):\/\/(.*)\/(.*)/',
    $url, $matches4);
    $url = trim($matches4[2]);
preg_match('/(.*)\/(.*)/',
    $url, $matches5);
    $url = trim($matches5[1]);
$url = 'http://'.$url.'/update_comments.php?theid='.$theid;
$myresult = file_get_contents($url);
?>


One way would be to use a javascript callback:

<script src="jsCommentsCallback.php?id=<?php echo $post->ID; ?>" type="text/javascript" defer="defer" />

Load the javascript at the bottom of the page and have the php file (ie jsCommentsCallback.php) hit your $url, using $_GET['id']. If you need to do something with the content of update_comments.php, you can package it up in JSON and send it back to the browser to insert into the page or whatever.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜