Slowness in restricted file

Asked

Viewed 99 times

1

I have a file in php.php process that receives information in looping via jquery and makes a request to another page with Curl, this file does something around 4 to 5 thousand requests one by one, everything worked much more after I restricted access to the file using sessions through code:

ob_start();
session_start();

if(isset($_SESSION["numlogin"])){
    $n1 = $_GET["id"];
    $n2 = $_SESSION["numlogin"];
    if($n1 != $n2) { }

}else{
    header("location:index.html");
}

The return of the data was slow before returning on screen 2 up to 3 json arrays per second more now every 8 seconds returns an array only, I believe it is because it performs the Session check on each request how can I improve this ?

my php process.

<?php
error_reporting(0);

ob_start();
session_start();

if(isset($_SESSION["numlogin"])){
    $n1 = $_GET["id"];
    $n2 = $_SESSION["numlogin"];
    if($n1 != $n2) { }

}else{
    header("location:../../../index.html");
}


$dados = $_POST["entrada"];
$nome = explode("|", $dados)[0];
$data = explode("|", $dados)[1];

function GetStr($string, $start, $end){
$str = explode($start, $string);
$str = explode($end, $str[1]);
return $str[0];
}


$ch = curl_init();
$url = "";
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch,CURLOPT_USERAGENT,"Mozilla/5.0 (Macintosh; Intel Mac OS X 10.7; rv:7.0.1) Gecko/20100101 Firefox/7.0.1");
curl_setopt($ch, CURLOPT_COOKIEFILE, "cookies.txt");
curl_setopt($ch, CURLOPT_COOKIEJAR, "cookies.txt");
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLOPT_FOLLOWLOCATION, 1);
curl_setopt($ch, CURLOPT_POSTFIELDS, "nome=$nome&data=$data");
$resultado = curl_exec($ch);
curl_close($ch);

$valor = GetStr($resultado, '<a href="/logout.html" title="sair" class="btn btnSair">',"</a>"); 

if ($valor == "Sair") {
$dds = array("consulta 1" => $nome, "data" => $data, "resultado" => "existe");
} else {
$dds = array("consulta 4" => $nome, "data" => $data, "resultado" => "nao existe");
}

echo json_encode($dds);

ob_end_flush(); 

?>

  • If it is session handling that has slowed down processing, why not handle more than one request at the same time? Your jquery can’t put it all together and send it in one POST, for PHP to loop Curl?

  • The problem is that it would bring a lot to the server so even jquery already sends it line by line on the client side, so everything is balanced

1 answer

3


This is possibly happening because the session_start locks the simultaneous reading. Two processes cannot read the same file, so the same user cannot load two pages at the same time while the first Curl is not finished.


Alter:

session_start();

To:

session_start();
session_write_close();

This should be enough for other processes to read the same file. Using write_close just says it won’t record anything, but you can still read the session normally.

  • Thank you worked perfectly as expected

Browser other questions tagged

You are not signed in. Login or sign up in order to post.