0

I have a PHP script which opens a socket connection to a machine on the same network and waits for a response for up to 60 seconds then returns the result. This script is called upon using an ajax request on the "main" page which displays a message containing the result.

Problem is I want to be able to end the socket connection from the "main" page at any time, is this something that can be done? Or can anyone else think of a better way of doing this?

Thanks!

3
  • it's hacky, but you can send a second ajax call that fires a shell command to find and kill the process with the socket connection...assuming you can identify the correct process in the process list Commented Jul 16, 2020 at 23:09
  • You can't close the socket from the Client side. You will need a PHP Script that can be aware of the File Pointer and can then close the socket. This can then be called via AJAX from the Client. I can't recall if you can store a FP in a Session variable. Might be easier to get the Process ID of the Socket and then kill then via another script. Commented Jul 16, 2020 at 23:11
  • It would be best to edit your post and include a sample of your code. Commented Jul 16, 2020 at 23:15

1 Answer 1

1

Here is a really lite weight and untested theory.

open.php

<?php
session_start();
$_SESSION['fp'] = fsockopen($_GET['url'], 80, $errno, $errstr, 60);
// Do listen for 60 seconds, get data
while (!feof($_SESSION['fp'])) {
  echo fgets($_SESSION['fp'], 128);
}
fclose($_SESSION['fp']);
unset($_SESSION['fp']);
?>

close.php

<?php
session_start();
if(isset($_SESSION['fp'])){
  fclose($_SESSION['fp']);
  unset($_SESSION['fp']);
}
?>

JavaScript

$(function(){
  $("#go").click(function(){
    $.get("open.php", { url: $("#url").val() }, function(results){
      console.log(results);
    });
  });
  $("#stop").click(function(){
    $.get("close.php");
  });
});

The idea here is that the File Pointer is stored in a session variable, so it can be called upon by other scripts. Since you didn't provide an example, I cannot say if this will work for you. I have never tested it since I've never wanted a script to close the connection prematurely. I've wanted to remain open until I got all my data and then close at EOF.

Alternatively, you can do something similar with the Process ID. Each PHP Script gets a PID when running. Discussed more here: How to kill a linux process using pid from php?

Sign up to request clarification or add additional context in comments.

1 Comment

While I didn't use the exact answer above it did lead me to a great idea that I just didnt consider before. I set a session variable to be true at the start of the page, and then made a button on the page which when clicked would load a php page to set that session variable to false. The script handling the socket connection would close the connection then on its end whenever a response was received OR when the session variable was false. Such a simple idea - thanks for the tip!

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.