Jump to content

Cannot execute another php script (Using XAMPP on Windows 10)


Go to solution Solved by requinix,

Recommended Posts

Hi,

 

I am a newbie at php and I recently tried making a small php configuration that runs on the localhost. 

execution.php

<?php
	echo "first script has been executed";
	exec('execution2.php');
?>

execution2.php

<?php
	echo "Second script has been executed";
?>

The script is designed to call another php file whereas on the web page I would expect seeing, "first script has been executed" and "Second script has been executed". I am honestly not sure how the execution method is supposed to work however I am not planning on using "include" or "require" since they do not meet my criteria for another project. I am using xampp localhost server on a windows 10 computer. I tried entering "localhost:8080/dir/execution.php" however it did not work. 

 

Any help would be appreciated, thanks!

Link to post
Share on other sites
18 minutes ago, denerex said:

I am not planning on using "include" or "require" since they do not meet my criteria for another project.

You're going to have to explain that, because 99.9% of the time the correct solution is include/require.

Link to post
Share on other sites
13 minutes ago, requinix said:

You're going to have to explain that, because 99.9% of the time the correct solution is include/require.

My main goal is to have one  php script run two other scripts with both 10 second delays. I need both php files to run at once however when I try this with include/require it only  executes one at a time and waits for one file to be finished. I tried using the solution from this site however the exec() method does not work as intended. 

Original project:

<?php
	echo "Timer begins...";
	include('timer1.php');
	include('timer2.php');
	
?>

 

<?php
	sleep(10);
	echo "Response from script 1";
?>

 

<?php
	sleep(10);
	echo "Response from script 2";
?>

 

Link to post
Share on other sites

What are these two pieces of code that have to run at the same time? Do they have output? Where is the output supposed to go? Is this something that would be better served by running two concurrent AJAX requests on the client side?

Link to post
Share on other sites
1 hour ago, requinix said:

What are these two pieces of code that have to run at the same time? Do they have output? Where is the output supposed to go? Is this something that would be better served by running two concurrent AJAX requests on the client side?

Just curious, since you mentioned Ajax, are most of these threads about JS web development? Because if so, I do indeed have a solution for requesting on the client side but it is in another  framework called Flutter. If it's necessary I can repost but with an AJAX based solution and the whole problem.

 

I  have made a client side program and  so far even on the client side two php scripts with a delay will only execute one at a time.  My original plan is to have a php task loop for 30 minutes and every second it will check my mysql database for a value change. (This is prob a terrible practice). Another script is supposed to wait 10 minutes and once that time lapsed it will destroy a row in the database. Both of the scripts are very large so I decided to make a simpler configuration. 

Link to post
Share on other sites
  • Solution
33 minutes ago, denerex said:

Just curious, since you mentioned Ajax, are most of these threads about JS web development? Because if so, I do indeed have a solution for requesting on the client side but it is in another  framework called Flutter. If it's necessary I can repost but with an AJAX based solution and the whole problem.

You mean are most questions we get here about Javascript? No, it's mostly PHP, but it's mostly PHP in a web context. As in PHP is running on a website and people are visiting it in their browser. Which means Javascript is an option. But sometimes there are non-web PHP questions.

I don't know which one of those this thread is yet...

 

Quote

I  have made a client side program and  so far even on the client side two php scripts with a delay will only execute one at a time.  My original plan is to have a php task loop for 30 minutes and every second it will check my mysql database for a value change. (This is prob a terrible practice). Another script is supposed to wait 10 minutes and once that time lapsed it will destroy a row in the database. Both of the scripts are very large so I decided to make a simpler configuration. 

If you want to run something every X minutes then the standard answer is to use cron: every *nix server has it, it runs in the "background", meaning it's not driven by or reliant upon users taking specific actions (such as "keeping the browser open so something can do AJAX requests"), and you can still do just about anything you want with PHP.

For the 30 minutes one, you make a script that runs whatever database queries it needs. I don't know what it's supposed to do if the value changes? Then you tell the server (exact steps vary) that you want to run some command-line program (ie, PHP with the path to your script) every 30 minutes.
For the 10 minutes one, you make a script that runs whatever database queries it needs. Then you tell the server you want to run a second program (also PHP) every 10 minutes.

  • Like 1
Link to post
Share on other sites

It sounds like you might just need to re-consider your design.

29 minutes ago, denerex said:

My original plan is to have a php task loop for 30 minutes and every second it will check my mysql database for a value change.

If it's the client that's interested in whatever this change is, the typical solution to this is to just have the client make a request periodically.  One name for this is Ajax Polling.  You make an Ajax request to your change detection script.  The script will check for the change in a loop and return when a change is detected.   You could wait 30 minutes, but it's more typical to wait 30-sec to a minute then return a "No changes" response.  This way your web-server's available threads don't get used up waiting.

If the change detection is only relevant to the server and not the client, then either cron or a service would be the more appropriate implementation.

34 minutes ago, denerex said:

Another script is supposed to wait 10 minutes and once that time lapsed it will destroy a row in the database.

This sounds like something that's more typically done with a cron job.  Your database row would contain a timestamp of when it was created or when it needs to be destroyed.  You then schedule a script via cron (or some other scheduler) that runs every minute and deletes any rows that have expired.  Similar to above, you don't really want to make a request to your server that just waits for 10 minutes as it'll tie up resources and it's particularly reliable anyway (might timeout / get killed).

 

In regard to your original post, multi-tasking is generally handled by using multiple requests rather than one request that executes sub-scripts.  It's possible to execute sub-scripts concurrently like you want, but it's somewhat complicated to do.  Sending separate requests is much easier.  Browsers do have a limit to how many concurrent requests they will make that you need to keep in mind though.

 

 

  • Like 1
Link to post
Share on other sites

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.