Pipes in PHP
Pipes in PHP
Pipes
• The popen() function used to open a pipe to the program
specified by the user using the command parameter.
• It returns a file pointer which is identical to that returned
by fopen(), but it is unidirectional in nature.
• It can be only used for reading or writing. The popen()
pointer can be used with fgets(), fgetss(), and fwrite().
• The file pointer initiated by the popen() function must be
closed with pclose().
Pipes
Syntax:
popen(command, mode);
• Parameters Used:
The popen() function in PHP accepts two parameters.
• command : It is a mandatory parameter which specifies the
command to be executed.
• mode : It is a mandatory parameter which specifies the
connection mode such as read only(r) or write only(w).
<?php
$myfile=popen("/ls","r");
echo $myfile. gettype($myfile);
if($myfile==false)
echo"pipe opening problem";
else
{
if(($file_read=fread($myfile,4192))==false)
echo "file reading problem";
else
{
echo $file_read;
pclose($myfile);
}
}
?>
Running commands using exec ()
• exec — Execute an external program or command (terminal
commands)
?>
system
system — Execute an external program and display the output.
system ( string $command , int &$result_code = null )
• Session variables are set with the PHP global variable: $_SESSION.
• The session_start() function must be the very first thing in our
document. Before any HTML tags.
Example
<?php
session_start();
?>
<html>
<body>
<?php
$_SESSION["user"] = “Admin";
echo "Session information are set successfully.<br/>";
?>
<a href="session2.php">Visit next page</a>
</body>
</html>
File: session2.php
<?php
session_start();
?>
<html>
<body>
<?php
echo "User is: ".$_SESSION["user"];
?>
</body>
</html>