在php中使用exec设置系统时钟
我正在使用一个 php 脚本,它从浏览器获取时间戳值并尝试设置系统时钟。我在我的 mac mini 中使用 mamp box。如何通过 php 脚本设置 mac mini 系统时钟。
我尝试使用 shell_exec, exec, system 来实现此目的,但它不起作用。
例如,我的 php 脚本看起来像这样,
<?
$timestamp = $_GET['timestamp'];
exec('date '.$timestamp);
?>
当我在终端中尝试相同的日期设置命令时,我看到时钟在变化。 ( sudo 日期时间戳 ),但我不知道如何通过执行特权命令的 php 脚本来实现它。请帮忙。
谢谢, 斯里尼。
I am using a php script which takes a timestamp value from browser and tries to set the system clock. i m using mamp box in my mac mini. how do i set the mac mini system clock through php script.
I tried using shell_exec, exec, system for achieving this , but it doesn't work.
Example, my php script looks something like this,
<?
$timestamp = $_GET['timestamp'];
exec('date '.$timestamp);
?>
When i try the same date set command in terminal i see the clock changing. ( sudo date timestamp ), but i dont know how to achieve it through php script executing a privileged command. Kindly help.
Thanks,
Srini.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
更改系统时间通常仅限于根级帐户。 PHP 将在网络服务器的用户 ID 下运行,该用户 ID 不应该是 root。为此,您必须使用 sudo 让网络服务器以提升的权限运行。
但是,为什么需要这样设置时间戳呢?您基本上是将 url 上传入的一些随机数据传递给 shell 命令。如何阻止某人提交
并破坏网络服务器有权访问的服务器上的每个文件?
如果您尝试在服务器之间同步时间,请使用 ntp,它专为时钟同步而设计。
Changing the system time is usually restricted to root-level accounts. PHP will be running under the user id of the webserver, which SHOULD NOT BE root. You'll have to use
sudo
to let the webserver run with elevated privileges for this purpose.However, why do you need to set timestamps like this? You're basically passing some random data passed in on a url to a shell command. What's to stop someone submitting
and nuking every file on your server that the webserver has access to?
If you're trying to sync times between servers, then use ntp, which is designed for clock synching.
首先,请清理您的输入。在这种情况下,请清理 $_GET['timestamp']。确保它采用
日期
可以理解的格式。确保它使用 escapeshellarg。您可以使用 exec('sudo date '.escapeshellarg($timestamp));。但请确保您的 Apache 用户有权使用 sudo 而无需密码。您可以在
/etc/sudoers
中指定仅允许 root 执行日期命令。这样比较安全。另一种选择是创建您自己的 shell 脚本并仅允许对您的脚本执行 sudo。
First of all, do sanitize your input. In this case, sanitize $_GET['timestamp']. Make sure it is in the format
date
will understand. Make sure it is using escapeshellarg.You can use
exec('sudo date '.escapeshellarg($timestamp));
. But make sure your Apache user has right to sudo without passwords. You can specify in/etc/sudoers
to allow ONLY date command to be executed by root. It is safier.Another option is to create your own shell script and allow to sudo only your script.