HTTP Function in PHP

Introduction

The PHP HTTP functions allow you to manipulate information sent to the browser by the web server and the purpose of the HTTP extension is to provide a convenient and powerful set of functionality for major applications. The HTTP functions are part of the PHP core. There is no installation necessary to use these functions.

In this article I describe the PHP HTTP functions header, headers_list, headers_sent, setcookie and setrawcookie.

PHP header() function

The PHP HTTP "header" function sends a raw HTTP header and this function returns no value. Basically when you request an internet page be brought back to your browser, you are not simply conveying back the web page. You're additionally conveying back something referred to as a HTTP Header. This is often some additional info, like the sort of programme creating the request, date requested, whether to display it as a Hypertext Markup Language document, however long the
document is, as well as much else.

Syntax

header(string, replace, httpRequestCode)

Parameters of the header function

The parameters of the function are:

Parameter Description
string It specifies the header string to send.
replace It indicates whether the header should replace the previous header or add a second header.
httpRequestCode It forces the HTTP response code to the specified value.

Example

An example of the function is:
 

<?PHP

header("Location: http://www.c-sharpcorner.com/");

?>

Output

header-function-in-php.jpg

PHP headers_list() function

The PHP HTTP "headers_list" function returns a list of response header sent (or ready to be sent) to the client and this function returns a numerically indexed array of headers.

Syntax

 

headers_list()

Example

An example of the function is:
 

<?php

setcookie('PQR', 'XYZ');

header("MCNSolution: abc");

header('Content-type: text/plain');

var_dump(headers_list());

?>

Output

headers-list-function-in-php.jpg

PHP headers_sent() function

The PHP HTTP "headers_sent" function checks if or where headers have been sent and this function will return false if no HTTP headers have already been sent otherwise returns true.

Syntax

 

headers_sent(file, line)

Parameters of the headers_sent function

The parameters of the function are:

Parameter Description
file It is an optional parameter so if the file and line parameters are set then headers_sent() will be put in the PHP source file
name and the line number where output was started in the file and line variables.
line It specifies the line number of where the output started.

Example

An example of the function is:
 

<?php

if (!headers_sent())

{

header('Location: http://www.c-sharpcorner.com/');

exit;

}

if (!headers_sent($filename, $linenum))

{

header('Location: http://www.c-sharpcorner.com/');

exit;

}

else

{

echo "Headers sent in $file on line $line";;

exit;

}

?>

Output

headers-sent-function-in-php.jpg

PHP setcookie() function

The PHP HTTP "setcookie" function sends a cookie and in this function, if output exists prior to calling the function, setcookie() will fail and returns false and if the setcookie() successfully runs, then it will return true.

Syntax

 

setcookie(name,value,expire,path,domain,secure)

Parameters of the setcookie function

The parameters of the function are:

Parameter Description
name It specifies the name of the cookie.
value It specifies the value of the cookie.
expire It specifies when the cookie expires.
path It specifies the server path of the cookie.
domain It specifies the domain name of the cookie.
secure It specifies whether or not the cookie should only be transmitted over a secure HTTPS connection.

Example

An example of the function is:
 

<?php

$cvalue = "Cookie Value";

// send a simple cookie

setcookie("TestCookie",$cvalue);

echo "<pre>";

//retrieve cookie value

print_r($_COOKIE);

?>

Output

setcookie-function-in-php.jpg
 

PHP setrawcookie() function

The PHP HTTP "setrawcookie" function sends a cookie without urlencoding the cookie value and this function returns true on success or false on failure.

Syntax

setrawcookie(name,value,expire,path,domain,secure)

Parameters of the setrawcookie function

The parameters of the function are:

Parameter Description
name It specifies the name of the cookie.
value It specifies the value of the cookie.
expire It specifies when the cookie expires.
path It specifies the server path of the cookie.
domain It specifies the domain name of the cookie.
secure It specifies whether or not the cookie should only be transmitted over a secure HTTPS connection.

Example

An example of the function is:
 

<?php

setrawcookie("User", "C-sharpcorner", time()+3600);

// retrive a rawcookie value

echo "<pre>";

print_r($_COOKIE);

?>

Output

setrawcookie-function-in-php.jpg

Up Next
    Ebook Download
    View all
    Learn
    View all