且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

在URL(php)中传递变量& file_get_contents()

更新时间:2023-02-25 15:00:54

当然.

如果您找出变量的模式,则有可能.

如果您注意到模式是数字,则可以尝试如下操作:

您不想显示在代码或输出中不相关的任何内容,并且您不想即时进行过滤,因为这只会增加延迟.在目标计算机上的get.php中:

if(!empty($_GET['a']) and is_numeric($_GET['a'])){

$id = $_GET['a'];
$sql = "select contents from database where id='$id'";
$results = mysqli_query($sql);
$row_cnt = mysqli_num_rows($result);
if ($row_cnt == 1){

   while ($row = mysqli_fetch_array($results)){
    echo $row['contents'];
   }
} elseif (empty($row_cnt) {
  echo "No results";
} else {
  echo "Too many records.";
}

} else {
 die;
}

在进行采矿的机器(机器2)中:

$contents = '';

for($x=0;$x<150;$x++){
  $contents.=file_get_contents('http://example.com/get.php?a='.$number);
}

echo $contents;

在通过网络打开URL时,请记住:

  1. 授予 allow_url_fopen 必要时进行访问
  2. 添加延迟,这样您就不会占用目标服务器的资源
  3. 确认您没有违反某人的 robots.txt 文件
  4. 传递适当的标头(包括user_agent),以便不会禁止您的服务器
  5. 如果要在Apache下以网页形式运行脚本,请增加运行时间限制.

cURL 对此更合适,并且包含许多高级功能. /p>

更新-但这可能不是一个好主意

上面的示例仅用于一两个页面,而不是用于在重新显示内容时查看网站上的内容.我误会了,以为您正在一次(一次)对站点进行数据挖掘.

局域网 在局域网上,显示来自一台服务器在另一台服务器上的内容的速度应该相当快,并且即使您正在挖掘内容,网站也应该(按照现代标准)相当快地运行.

您需要将目标站点的域和ip添加到hosts文件,以使系统在每次调用该函数时都不会执行DNS查找(如果禁用了缓存).

相同的框 如果它们在同一台计算机上,并且如果没有某种形式的暂停,则可能会使系统超载.

WAN 如果您位于两个服务器场中相隔一定距离的两个单独的网络中,则跃点数将极大地影响脚本的性能.就长时间运行实时生产服务器而言,这不是一个好主意.大多数人不会等待几秒钟来加载页面.

此外,您还希望从正在开采的服务器的统计信息中筛选出挖掘服务器的ip,因为看起来目标计算机上的所有流量都来自一个位置.

由于它们位于两个不同的盒子上,因此您将要确保IP表(防火墙)之类的文件不会锁定目标服务器上的所有访问权限,因为您可能会通过相同的IP太快地重复连接到该站点.如果您不能同时控制两个网络,那么各种各样的事情也可能会阻止重复访问,例如防火墙和路由器.许多Web主机不喜欢重复的高流量.另外,如果您不小心配置了任何一个框,那么带宽可能会受到损失.

数据库复制 如果要从数据库中提取内容,则可能需要调查数据库复制并在每台计算机上保留该数据库的两个副本.然后,您将像加载其他文件一样简单地加载内容.

Is there a way to pass a variable in a URL with file_get_contents() and have file_get_contents() retrieve dynamic content that is based on the value of the variable passed?

For example, let's say I have the following code on a page on Website A:

$contents=file_get_contents('http://example.com/get.php?a='.$number);
echo $contents;

where $number is generated on Website A (values can be 1, 2, 3, etc.)

Then on example.com, get.php is hosted. Is it possible to retrieve different content from get.php based on the value of the variable passed? For example, if a=1 then a certain part of get.php would be fetched, but if a=2 and different part of get.php would be fetched, etc. Is this possible?

Certainly.

It is possible if you figure out the pattern for the variables.

If you notice that the pattern is a number you might try something like this:

You don't want to show anything that's not going to be pertinent in the code or output and you don't want to get into filtering on the fly because it will only add delay. In get.php on the target machine:

if(!empty($_GET['a']) and is_numeric($_GET['a'])){

$id = $_GET['a'];
$sql = "select contents from database where id='$id'";
$results = mysqli_query($sql);
$row_cnt = mysqli_num_rows($result);
if ($row_cnt == 1){

   while ($row = mysqli_fetch_array($results)){
    echo $row['contents'];
   }
} elseif (empty($row_cnt) {
  echo "No results";
} else {
  echo "Too many records.";
}

} else {
 die;
}

In the machine doing the mining (machine 2):

$contents = '';

for($x=0;$x<150;$x++){
  $contents.=file_get_contents('http://example.com/get.php?a='.$number);
}

echo $contents;

Remember when opening a URL over the web that you need to:

  1. grant allow_url_fopen access if necessary
  2. add a delay so you're not overwhelming the target server's resources
  3. verify that you're not in violation of someone's robots.txt file
  4. pass proper headers including user_agent so your server is not banned
  5. increase the run time limits if you're running the script as a webpage under Apache.

cURL is much more appropriate for this and contains many advanced features.

Update - but it's probably not a good idea

The above example would be for just one or two pages, not for viewing the contents on a website as re-showing the contents. I misunderstood and thought you were datamining a site (one-time).

LAN On a local network LAN showing the contents from one server on another should be pretty fast and the websites should perform fairly quickly (by modern standards) even though you're mining content.

You'll want to add the target site's domain and ip to the hosts file so the system doesn't perform a DNS look-up (if caching is disabled) everytime the function is called.

Same box If they were on the same machine you might overload the system if some sort of pauses aren't in place.

WAN If you're on two separate networks in two server farms separated by some distance the number of hops will greatly impact the performance of the script. This is not a good idea in terms of running a live production server for any length of time. Most people will not wait a couple of seconds for a page to load.

Additionally you'll want to filter the mining server's ip out of the stats for the server being mined as it will look like all of your traffic on the target computer comes from one location.

Since they are on two different boxes you'll want to make sure something like IP tables (the firewall) won't lock all access on the target server because you might connect repeatedly to the site too quickly from the same IP. If you're not in control of both networks all sorts of things can also block repeated access such as firewalls and routers. Many web hosts do not like repeated high volumes of traffic. Also you may be penalized for bandwidth if you accidentally misconfigure either box.

Database Replication If you're pulling contents from a database you may want to look into database replication and keep two copies of the database on each machine. Then you would simply load the contents as you would any other file.