PHP-以编程方式写入主机文件 [英] PHP - Writing to Host file programmatically

查看:94
本文介绍了PHP-以编程方式写入主机文件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在 localhost Xampp 安装中完成项目的实现

I am doing my project implementation in localhost Xampp installation

我的项目的一部分是,

当用户在我的页面上注册时,系统将要求他提供所需的子域,然后,其URL将是:

When the user sign up on my page he will be asked for his desired sub domain and from then on his URL will be :

chosensubdomain.mysite.com

chosensubdomain.mysite.com

一切正常,除了子域,我必须手动在Windows主机文件中添加条目,如下所示

Everything works perfectly , except for sub domain i have to manually add the entries in windows host file as follows

127.0.0.1       chosensubdomain.mysite.com   abc.mysite.com  xyz.mysite.com

我想要的是表单提交主机文件时应添加的条目

What i want was when the form submits the host file should be added up with a entry

我不确定是否可以使用PHP以编程方式在Windows Host文件中进行输入.

I am not sure that whether it is possible to make entry in Windows Host file programmatically using PHP .

请帮助我与这个伙伴...

Kindly help me out with this mates ...

推荐答案

虽然这很有可能,但是一种更好的省时方法是使用您选择的编程语言在服务器上动态创建子文件夹,然后使用.htaccess文件将http://foo.com/bar重写为http://bar.foo.com. 如果您在实时服务器上,或者在本地计算机上使用 Zpanel ,它应该会对您有所帮助(先进的dns控件),否则您可以在主机文件中手动添加一个包罗万象(*)子域.

While this is very possible, a better, time saving approach will be to use your programming language of choice to dynamically create sub-folders on the server then use a .htaccess file to rewrite http://foo.com/bar to http://bar.foo.com. This should help you if you're on a live server or you use Zpanel on a local machine (For ease of advanced dns control) else you can manually add a catch-all (*) sub-domain in your host file.

*.foo.com       IN  A       127.0.0.1

如果仍然想以其他方式执行此操作,则必须使用PHP的I/O函数将文本动态附加到主机文件中.

If you still want to do it the ol' way, you have to use PHP's I/O functions to append text dynamically to your host file.

$sub_domain = "   foo.bar.com";
$append = file_put_contents('C:\Windows\System32\drivers\etc\hosts', $sub_domain.PHP_EOL , FILE_APPEND | LOCK_EX);

我实际上认为可以更好地回答您的问题.

I actually think this answers your question better.

这篇关于PHP-以编程方式写入主机文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆