Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8888157
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 14, 20262026-06-14T21:58:24+00:00 2026-06-14T21:58:24+00:00

I am using stream_get_line to store some php output in a variable, while I’m

  • 0

I am using stream_get_line to store some php output in a variable, while I’m running a telnet session via fsockopen.

However, my second server does not run PHP5, which is disabled the ability to use stream_get_line. Is there any alternative for PHP 4.3?

I heard that fgets is almost the same, but I don’t seem to get it to work exactly like stream_get_line.

Code:

...
# opening connection    
$fp = @fsockopen($ip, 23, $errno, $errstr, 8);
    # loggin in
    fputs($fp, "$user\r");
    usleep(250000);
    fputs($fp, "$password\r");

# getting information
fputs($fp, "show info\n");
usleep(250000);
fputs($fp, "show info 2\n");
usleep(250000);
fputs($fp, "show info 3\n");
usleep(250000);
fputs($fp, "show info 4\n");
usleep(250000);
fputs($fp, "?\n");

$content = stream_get_line($fp, 0, "?");
$contentvalues = array(
    1 => substr($content, 130, 3),
    2 => substr($content, 180, 3)
);

fclose($fp);
...

(I am storing specific parts of my output in the $contentvalues variable.)

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-14T21:58:25+00:00Added an answer on June 14, 2026 at 9:58 pm

    From the docs:

    This function is nearly identical to fgets() except in that it allows
    end of line delimiters other than the standard \n, \r, and \r\n, and
    does not return the delimiter itself.

    From the comments:

    when fgets reads some bytes from socket, where EOF is reached, it
    returns bool(false) same as stream_get_line

    BUT if remote client drops connection, and server script will try to
    read some data with function fgets, function will return bool(false),
    and stream_get_line will return string(0) ""

    so you can detect remote client disconnection with stream_get_line,
    and cannot with fgets

    There’s also some dithering about which function is faster, but it seems to be dependant on the version of PHP, the day of the week, and what the commenter had for dinner the previous night.

    edit

    Judging by your response to Steffen’s answer you’re hung up on the fact that fgets() does not take a third parameter as a delimiter. Applying a basic input loop and checking the string will get you there. Also, in Steffen’s defense, you were never quite clear on in your question, stating only that it doesn’t "work exactly like stream_get_line".

    <?php
    $delim = '?';
    $buf = 4096;
    
    $fp = @fsockopen($ip, 23, $errno, $errstr, 8);
    
    // ... yadda yadda yadda ... //
    
    $content = '';
    while( $part = fgets($fp, $buf) ) {
        $ind = strpos($part, $delim);
        if( $ind !== false ) {
            $content .= substr($part, 0, $ind);
            break;
        }
        $content .= $part;
    }
    

    Also, even with stream_get_line() you should be using a loop to get the input as a length parameter or 0 does not mean "unlimited", but rather will use one of PHP’s defaults which is 8192 bytes.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Problem: I'm streaming my video from a php file with stream_get_contents(); using Flowplayer as
I'm using SharpDX and XAudio2 in a Windows Store app. I'm trying to create
I'm a php developer. In .NET we are able to store data in server
I'm trying to upload a file using PHP. My HTML page with the form
I would like to declare multiple variables using the output of my script and
I'm encountering a problem while trying to serialize a multi-dimensioned array of integers via
I'm trying to establish an interactive SSH connection to a remote server using PHP
How can I collect real-time tweets using Ruby and store them in MongoDB? Please
I'd like to convert a HBitmap to a video stream using libavcodec. I get
I want to get the video stream from my webcam using python and OpenCV,

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.