execute php script in command line from web page

8.7k views Asked by At

I'm green at running php in the command line, and need to find a way to execute a php script by loading a web page in my browser. I don't need any return values, but only need to make the script to run/execute. So, once "page.php" is loaded in the browser, a script which is inside page.php will begin running in terminal/cli.

I've tried doing this by adding the line <?php exec('php script.php') ?>, but it doesn't work. Is this possible?

UPDATE:

Provide some code to give insight/clarity on my setup to help with testing. Can the script be ran by putting it in the body like below, or does it need to be called explicitly somehow?

<!doctype html>
<html>
<head>
<meta charset="UTF-8">
<title>Web Page</title>
<link href="style.css" rel="stylesheet" type="text/css">
<?php include_once 'check_login_status.php'; ?>
</head>
<body>
<?php exec('php /Applications/XAMPP/htdocs/site_root/script_to_run.php'); ?>
<div id = "wrapper">
    <div id = "nav"><?php include_once 'navigation.php'; ?></div>
    <div id = "top"><?php include_once 'top.php'; ?></div>
    <div id = "main"></div>
</div>
</body>
</html>
1

There are 1 answers

0
rjdown On BEST ANSWER

You have to understand that exec doesn't have access to all the usual environment variables that would be in, for example, an SSH prompt. You need to provide the full path to PHP in your command. Probably something like

<?php exec('/usr/bin/php script.php'); ?>