How I can find a specific word in a external page using PHP? This page is protected by a login area. Is it possible to do something?
<?php
$v = file_get_contents("http://it.website.com");
echo substr_count($v, 'web'); // number of occurences
//or single match
echo substr_count($v, ' abcd ');
?>
My problem is to pass the login automatically..
Login form:
<form name="login" action="/login.php" method="post" onsubmit="return check(this);">
<div class="row">
<div class="form-group">
<div class="col-md-12">
<label>Email o Nome utente</label>
<input type="text" value="" name="user_email" class="form-control input-lg">
</div>
</div>
</div>
<div class="row">
<div class="form-group">
<div class="col-md-12">
<label>Password</label>
<input type="password" value="" name="user_pass" class="form-control input-lg">
</div>
</div>
</div>
<div class="row">
<div class="col-md-12">
<input type="submit" value="Accedi" name="login" class="btn btn-primary pull-right push-bottom" data-loading-text="Loading...">
<input type="hidden" name="mode" value="checklogin" id="login">
</div>
</div>
You could try this code snippet (PHP 5+ needed, for older versions ask please).
It sends a POST request as it is done when the Accedi button is pressed:
$url = 'http://it.website.com/login.php';
$para = array(
'user_email' => 'myname@example.com',
'user_pass' => 'MySuperSecretPassword',
'mode' => 'checklogin', // the hidden field in the form
);
$opts = array(
'http' => array(
'header' => "Content-type: application/x-www-form-urlencoded
",
'method' => 'POST',
'content' => http_build_query($para),
)
);
$ctxt = stream_context_create($opts);
$page = file_get_contents($url, false, $ctxt);
if ($page !== false) {
// $page contains the page returned by the web server
echo substr_count($page, ' abcd ');
echo substr_count($page, 'web');
}
Note: The user_email, user_pass, the hidden mode fields and the url part /login.php are taken from the <form>…</form> code. Maybe it is a good idea to have a look at the check(this); function.