简体   繁体   中英

execute php script in command line from web page

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>

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'); ?>

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM