PHP Sending Variables to File_Get_Contents()

Passing variable in a URL (php) & file_get_contents()

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.

Get variable in file_get_contents('file.php')

file_get_contents() reads the specified file byte by byte but does not interpret it's contents. What you are searching for is the include() directive:

<div id="resultContent">
<?php include('file.php') ?>
</div>

Pass variables to PHPMailer get_file_contents

one way you could do is, you could add placeholders in your welcome.php and replace those placeholders with actual values, once you get the contents, using str_replace() function, like:

...
$searchArr = ["YOUR-PLACEHOLDER-FIRST", "YOUR-PLACEHOLDER-SECOND"];
$replaceArr = [$yourFirstVariable, $yourSecondVariable]

$body = file_get_contents('emails/templates/carer/welcome.php');
$body = str_replace($searchArr, $replaceArr, $body);
...

The palceholders YOUR-PLACEHOLDER-FIRST and YOUR-PLACEHOLDER-SECOND are to be added to welcome.php file



Related Topics



Leave a reply



Submit