0% found this document useful (0 votes)
485 views

Pipes in PHP

Pipes allow passing of data between programs in PHP. The popen() function opens a pipe and returns a file pointer that can be used to read from or write to the pipe. Pipes must be closed with pclose(). exec(), system(), and passthru() functions execute external programs and commands. Sessions in PHP provide a way to store and access data across multiple pages using session variables accessible via the $_SESSION superglobal. Session data is stored on the server and identified using a session ID cookie.

Uploaded by

Raju Puppala
Copyright
© © All Rights Reserved
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
485 views

Pipes in PHP

Pipes allow passing of data between programs in PHP. The popen() function opens a pipe and returns a file pointer that can be used to read from or write to the pipe. Pipes must be closed with pclose(). exec(), system(), and passthru() functions execute external programs and commands. Sessions in PHP provide a way to store and access data across multiple pages using session variables accessible via the $_SESSION superglobal. Session data is stored on the server and identified using a session ID cookie.

Uploaded by

Raju Puppala
Copyright
© © All Rights Reserved
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
You are on page 1/ 15

PIPES

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)

• exec ( string $command , array &$output = null , int &$result_code =


null ) :
• Returns output string on success , false on failure.
<?php
// (on a system with the "iamexecfunction" executable in the path)
$result=array();
echo exec('date',$result);
print_r($result);

?>
system
system — Execute an external program and display the output.
system ( string $command , int &$result_code = null )

• Returns string or false


• command
• The command that will be executed.
• result_code
• If the result_code argument is present, then the return status of the executed
command will be written to this variable.
<?php
system('date');
?>
passthru
• passthru — Execute an external program and display raw output

passthru ( string $command , int &$return_var = ? )

The passthru() function is similar to the exec() function in that it


executes a command.
This function should be used in place of exec() or system() when the
output from the Unix command is binary data which needs to be
passed directly back to the browser.
<?php
// (on a system with the "iamexecfunction" executable in the path)
//$result=array();
passthru('dir');
?>
Sessions in PHP
Introduction to sessions
• A session is a way to store information (in variables) to
be used across multiple pages.
• PHP session is used to store and pass information from one page to
another temporarily (until user close the website).
• session technique is widely used in shopping websites where we need
to store and pass cart information
• For e.g. username, product code, product name, product price etc from
one page to another.
PHP sessions
Start a PHP Session
• A session is started with the session_start() function.

• 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>  

You might also like