Php if page is loaded

Can PHP run after the page is loaded?

So PHP is executed server-side. But is it possible for PHP to be run after the page is loaded? To illustrate, if I had a value (stored in a file, lets say) that changed every second. When I hit a button that uses Javascript to ask PHP to get that value, does it display what the value currently is, or what it was at page load?

Well you can technically flush the output buffer and keep executing a script. But for what you want, research into ajax techniques.

3 Answers 3

I think you need to get one of those diagrams that show how basic HTTP and the web server works. It will make more sense to you, than explained in plain words here.

In the simplest possible case, the result of you typing some address and getting a web page with its contents can be summed up, due to a result of process in request/response relationship between your browser and a web server located somewhere in the world.

Plain HTML

In a less simpler way, think of it like this. basically, if a page is during a refresh phase, (meaning you clicked something and are waiting for a data to comeback) then, that means it is getting/loading the response from the web server. If the web server does not have PHP installed as a module, then the only thing it is waiting/loading (in many cases) is plain HTML content.

Читайте также:  Python time текущий час

With PHP

On the other hand, if we assume you have a file called index.php in your webserver, and have PHP is installed, in this case the web server will send everything that appears in-between to the PHP interpreter, first, then wait for it until PHP does its magic and send back to the server only the result.

So, in the above case, the webserver (ex: Apache, Nginx) does not care what is inside the opening and closing tags, and sends the entire code to the PHP interpreter, and PHP would compute that script according the way it understands it and sends only the computed result back to the server, as plain HTML. In this case the number 2 .

The role of AJAX.

AJAX (Asynchronous JavaScript and XML) is a technique used Javascript, to help you send requests and receive the response without having to load the page. This is usually done by using the browsers XHR object. So, there is no mystery in this whole shebang.

The above can be summed up simply in the following steps.

  • Enter foo.com browser sends a request to the server of foo.com
  • server/browser exchange messages server allows browser to aquire
  • information server sends index.php back to browser if
  • is found in the script, server sends all the codes inclosed in those
  • tags to the PHP interpreter The PHP interpreter, compiles the query
  • and sends the result as HTMl

Источник

How to check with is_page if 404.php is loaded?

I want to check if current page match at least one of two conditions. I don’t want to show js code into about page and into 404 page. For the rest of my pages i show the js code. So, I use ((!is_page(element1) AND !is_page(element2)) then show the js code. Element1 is a slug of one specific page and element2 is 404.php page. I don’t know the slug or name of 404 page to indicate in if statement.

I don’t get how a page could be a template element1.php and a 404 page in the same time ? It’s one or the other — 404 is by definition a ‘ressource not found’ so it does not use any template but the 404.php template.

I need a trigger that works if any of two conditions match. I display a search form for each page only if it is not a main page or if it is not a 404 page where other search form is included in body. my new code works well for my blog

Your description is inconsistent with the accepted answer, which has been edited (by you, @Yurij73) since @Simon published it. You say you want «at least one of two conditions» to match, but the answer requires both conditions to match. That is an || , as @Simon originally had it, not an && . This is going to lead to a lot of confusion. Please clean up the question.

Exactly as @Simon say 0 for any of two condition. That was a cible. I’m correcting my question now to expose question in a right way.

2 Answers 2

if ( is_page( 'element1.php' ) || is_404() ) < // do that if page is using template page element1.php or is 404 >

A 404 page probably doesn’t have a slug . It is created automagically like the tag and category pages archive pages

Now, is_page doesn’t tell you anything about the template being used, which you seem to be thinking about checking for when you say and «and element2 is 404.php page». is_page_template will. So.

if (is_page('element1') || is_page_template('404.php')) < // stuff >

Will trigger if page-slug == element1 is loaded or if a page is loading that uses 404.php . But 404.php is not a necessary theme file so looking for the template may not be what you want. You probably want to use is_404 as in Simon’s answer (which you should «accept» if you go with is_404 ).

Источник

how to make sure a PHP script is loaded one time

I have a cronjob system with PHP. I have a file named cron.php and wanna to check if it is not loaded, load it. it is very important to me that this file run only one time and I need the way how define it is already running. I can’t use any system functions like exec,system. do you have any solutions? NOTE: I run my script trough CURL and include_one and require_once don’t work for this case.

Do you mean that you want to avoid the cron job cron.php running if an existing one is already executing? (because that is way different that the answers you’re currently getting so you should clarify..)

You will have to store the last execution time of your script somewhere (database, text file). Then check the last execution time/date (userID, etc.) and execute the script only if some condition (timeout) is given.

9 Answers 9

You could use flock() to lock the php file itself, like this:

fp=fopen(__FILE__,'r'); if (!flock($this->fp,LOCK_EX|LOCK_NB)) < die('already running !'.PHP_EOL); >> function __destruct()< flock($this->fp,LOCK_UN); fclose($this->fp); > > $lock=new Lock(); // simulate some processing sleep(60); echo "END"; ?> 

@IVIR3zaM: if the script gets killed the lock also expires. (not sure if apache kills the script if it gets restarted, but i would think so)

right, forgot about flock. there were some discussions, how reliable flock is under different server configurations, but it seems it is pretty reliable now? — I too think apache kills running scripts when it’s restarted normaly

require_once will throw a PHP fatal error (will stop execution) if the file cannot be evaluated. include_once will throw a PHP warning (execution may continue).

// Require tasks.php to run once require_once 'path/to/tasks.php'; // Attempt to run tasks.php and only once include_once 'path/to/tasks.php'; 

Your problem is essentially equivalent to «Check if a php script is still running»

it is nice but not work correctly, in my script many works execute and may it take too long and pass from seconds, in this case my script thought it is not running and run again

if I understand you correctly, you want to prevent your cron.php script from getting started a second time by cron, it is not called from another PHP script? (in that case, require_once would be the right answer)

as I understand it, you need to store a marker that indicates that your script is running and remove that marker at the end of your script.

depending on your environment, you could either create a small file, i.e. .lock or store a status = locked entry in your database.

edit: here is a small code example using the file method:

 // if script reaches this point, it is not locked -> create a lock file_put_contents($path . '.lock', 'lockfile created at ' . now()); //. your code. //unlocking unlink($path . '.lock'); ?> 

yes you understood it. it called trough CURL. but your idea have a problem, maybe Apache get restarted and my script didn’t to end to unlink .lock file and then my system shutting down forever

If you are using cURL then I believe your are using cURL to request a page such as http://domain.com/cron.php . The machine requesting the script via cURL/wget/browser/etc has no way of knowing if the script is already being executed on the server. However, you can configure your cron.php script to run only once:

 // code goes here echo date("Y-m-d H:i:s") . ": cron job started\n"; sleep(30); echo date("Y-m-d H:i:s") . ": cron job ended\n"; // release the lock flock($fp, LOCK_UN); fclose($fp); 

The sample code uses PHP flock function. The LOCK_EX flag tells PHP that it needs to obtain an exclusive lock; i.e. no other process is allowed to access the file. The LOCK_NB tells PHP that it should not block (wait for the lock to be released) and return false immediately. Together, the two switches assure that a second process cannot lock the file while the first one has it locked.

Источник

How to know if the page was loaded from a popup window

Your client-side code would have to tell your server-side code that this is happening. When opening the «popup» window you could include a query string value indicating to the server-side code that it’s a «popup» window. Other than explicitly telling the server-side code, however, there’s no other way. It’s not really the server-side code’s concern in which window the client is displaying the content.

@mjayt: which is why that was a comment, to signify that it’s NOT an answer. I’m also not a big fan of unecessarily terse questions, with no context to figure out exactly what the end result should be. Terse question, terse comment/answer.

3 Answers 3

if you open popup yourself you can open window with query string like this:

then you can check this query string in php like this:

You cannot do it with PHP.

You may want to use `$_SERVER[‘HTTP_REFERER’] to determine how the user reached the page to determine if it’s a popup window.

If you have the option of using JQuery then you can use $(window).height(); to determine the size of the window.

PHP is server side, browser window are client side. there is no way to know this using only PHP. you need a client side language that can tell the difference, and pass it through to your PHP script.

But as was already said in the comments, there is no inherent difference between a popup and a ‘normal’ browser window. just as there is no difference between 2 instances of a browser and 2 tabs in the same browser window.

This question is in a collective: a subcommunity defined by tags with relevant content and experts.

Источник

Оцените статью