Vicky
Vicky

Reputation: 489

file_get_contents() Timeout in php

I know php doesn't give timeout option since it is running from server side. But my logic needs timeout. Please solve this case

My PHP: login.php :-

$forms_values = $_POST;
$url = "http://sample.com/sample?params=$forms_values";
$resp = file_get_contents($url);
// It takes much time if sample.com is down.
// so need to set time out here.
$response = json_decode($resp, true);
....
....
if ($response["auth"] == "yes"){
   header("index.php");
}

my html index.html :-

<form action="login.php" method="post" id="clientLogin">
    <div>
        <input id="username" placeholder="Email or Username" type="text" name="luser" size="30" value="">
    </div>
    <div>
        <input id="passwd" placeholder="Password" type="password" name="lpasswd" size="30" value="">
    </div>
<input class="btn" type="submit" value="Sign In">
</form>
<script type="text/javascript">
//This is not working. and obviously delay will happen in server code 
//and i cant get it in client side 

$(window).load(function () {
     var submit = false;
     $("#clientLogin").submit(function(e) {
         alert("me after 1000 mili seconds");
          setTimeout(function(){
              alert("me after 1000 mili seconds");
              submit = true;
              $("#clientLogin").submit(); // if you want            
          }, 15000);
          if(!submit)
              e.preventDefault();
     });
};
</script>

This javascript is not working. and obviously delay will happen in server code and i cannot watch it from client side.

so this is what i want. need to terminate submission process (from submit to rendering next page) if my file_get_contents() function is not responding for long.

Note: Please dont ask me to use curl, Because i am instructed to not use curl. and i cant get it in client side

Upvotes: 2

Views: 7925

Answers (5)

Francesco Orsi
Francesco Orsi

Reputation: 2089

if you don't receive the content in 5 seconds, $resp will be empty


$ctx = stream_context_create(['http'=> ['timeout' => 5]]); // 5 seconds
$resp = file_get_contents($url,null,$ctx);

Upvotes: 0

vitorio
vitorio

Reputation: 327

set_time_limit() does run globally, but it can be reset locally.

Set the number of seconds a script is allowed to run. If this is reached, the script returns a fatal error. The default limit is 30 seconds or, if it exists, the max_execution_time value defined in the php.ini.

When called, set_time_limit() restarts the timeout counter from zero. In other words, if the timeout is the default 30 seconds, and 25 seconds into script execution a call such as set_time_limit(20) is made, the script will run for a total of 45 seconds before timing out. I've not tested it, but you may be able to set it locally, resetting when you leave the

<?php
set_time_limit(0);  // global setting

function doStuff()
{
    set_time_limit(10);   // limit this function
    //  stuff
    set_time_limit(10);   // give ourselves another 10 seconds if we want
    //  stuff
    set_time_limit(0);    // the rest of the file can run forever
}

// ....
sleep(900);
// ....
doStuff();  // only has 10 secs to run
// ....
sleep(900);
// ....

Upvotes: 0

Paras Jain
Paras Jain

Reputation: 393

Alternatively, consider using cURL .

It provides the CURLOPT_CONNECTTIMEOUT parameter to specify the amount of time to wait while trying to establish a connection.

Upvotes: 0

Megan Fox
Megan Fox

Reputation: 435

Place the code the beginning of your script

ini_set('default_socket_timeout', 30); // 30 seconds

by default it is set to 60 seconds .

Upvotes: 1

KRONWALLED
KRONWALLED

Reputation: 1442

The default value for a timeout in file_get_contents() is 60 seconds. You can change this value through the default_socket_timeout setting. It's also doable in your code:

ini_set('default_socket_timeout', 2); // 2 seconds

Upvotes: 5

Related Questions