Posts Tagged ‘HTTP’

Php:HTTP

Monday, June 30th, 2008

Client/Server Request/Response
HTTP is a simple client/server protocol with stateless request/response sequences.

The Client HTTP Request
7 possible HTTP 1.1 request types: GET, PUT, POST, DELETE, HEAD, OPTIONS and TRACE.
Any number of HTTP headers can accompany a request.

GET /filename.php HTTP/1.0
Accept: image/gif, image/x-xbitmap, image/jpeg, image/pjpeg, image/png, */*
Accept-Charset: iso-8859-1,*,utf-8
Accept-Encoding: gzip
Accept-Language: en
Connection: Keep-Alive
Host: localhost
User-Agent: Mozilla/4.77 [en] (X11; U; Linux 2.4.5-pre4 i686; Nav)
The Server HTTP Response
HTTP/1.1 200 OK
Date: Mon, 21 May 2001 17:01:51 GMT
Server: Apache/1.3.20-dev (Unix) PHP/4.0.7-dev
Last-Modified: Fri, 26 Jan 2001 06:08:38 GMT
ETag: “503d3-50-3a711466″
Accept-Ranges: bytes
Content-Length: 80
Keep-Alive: timeout=15, max=100
Connection: Keep-Alive
Content-Type: text/html

PHP:Force a secure HTTP connection

Monday, June 30th, 2008

if (!($HTTPS == “on”)) {
header (”Location: https://$SERVER_NAME$php_SELF”);
exit;
}

Linux:Enable port 81 or other non-standard port under Redhat SELinux

Monday, June 30th, 2008

Enable Apache to bind port 81 or other non-standard port under Redhat SELinux
login as root
Add a port
[root@acer ~]# semanage port -a -t http_port_t -p tcp 81
Remove a port
[root@acer ~]# semanage port -d -t http_port_t -p tcp 81
List all enabled http ports
[root@acer ~]# semanage port -l | grep http

Linux:set up WGET http proxy

Monday, June 30th, 2008

set http_proxy=http://your_proxy_server:1080

PHP:How to disable PHP magic quotes effect

Monday, June 30th, 2008

set_magic_quotes_runtime(FALSE);
if (get_magic_quotes_gpc()) {
/*
All these global variables are slash-encoded by default,
because    magic_quotes_gpc is set by default!
(And magic_quotes_gpc affects more than just $_GET, $_POST, and $_COOKIE)
*/
$_SERVER = stripslashes_array($_SERVER);
$_GET = stripslashes_array($_GET);
$_POST = stripslashes_array($_POST);
$_COOKIE = stripslashes_array($_COOKIE);
$_FILES = stripslashes_array($_FILES);
$_ENV = stripslashes_array($_ENV);
$_REQUEST = stripslashes_array($_REQUEST);
$HTTP_SERVER_VARS = stripslashes_array($HTTP_SERVER_VARS);
$HTTP_GET_VARS = stripslashes_array($HTTP_GET_VARS);
$HTTP_POST_VARS = stripslashes_array($HTTP_POST_VARS);
$HTTP_COOKIE_VARS = stripslashes_array($HTTP_COOKIE_VARS);
$HTTP_POST_FILES = stripslashes_array($HTTP_POST_FILES);
$HTTP_ENV_VARS = stripslashes_array($HTTP_ENV_VARS);
if (isset($_SESSION)) {    #These are unconfirmed (?)
$_SESSION = stripslashes_array($_SESSION, ”);
$HTTP_SESSION_VARS = stripslashes_array($HTTP_SESSION_VARS, ”);
}
/*
The $GLOBALS array is also slash-encoded, but when all the above are
changed, $GLOBALS is updated to reflect those changes.  (Therefore
$GLOBALS should never be modified directly).  $GLOBALS also contains
infinite recursion, so it’s dangerous…
*/
}
function stripslashes_array($data) {
if (is_array($data)){
foreach ($data as $key => $value){
$data[$key] = stripslashes_array($value);
}
return $data;
}else{
return stripslashes($data);
}
}

PHP:smarty (php template engine) check for firefox useragent

Monday, June 30th, 2008

<!– Google FireFox –>
{if !eregi(”Firefox”,$smarty.server.HTTP_USER_AGENT)}

<div align=”center”>
{literal}
<script type=”text/javascript”><!–
google_ad_client = “pub-8425891275034886″;
google_ad_width = 110;
google_ad_height = 32;
google_ad_format = “110×32_as_rimg”;
google_cpa_choice = “CAAQ_-KZzgEaCHfyBUS9wT0_KOP143Q”;
//–></script>
<script type=”text/javascript” src=”http://pagead2.googlesyndication.com/pagead/show_ads.js”>
</script>
{/literal}
</div>
{/if}
<!– /Google FireFox –>
Note:
you should replace your own code between {literal} .. {/literal}
please check also google’s terms when you’ll get paid

PHP:PHP referrer ( referer ) - Tracking/Loging/Checking link popularity

Monday, June 30th, 2008

<?php
$referrer = @$_SERVER["REFERER"];
// another try to catch the referrer
if ( empty( $referrer ) && !empty( $HTTP_REFERER ) )
{
$referrer = $HTTP_REFERER;
}
echo “The referrer is <b>$referrer</b>”;
?>

PHP:HTTP authentication with PHP

Monday, June 30th, 2008

<?php
if (!isset($_SERVER['PHP_AUTH_USER'])) {
header(’WWW-Authenticate: Basic realm=”My Realm”‘);
header(’HTTP/1.0 401 Unauthorized’);
echo ‘Text to send if user hits Cancel button’;
exit;
} else {
echo “<p>Hello {$_SERVER['PHP_AUTH_USER']}.</p>”;
echo “<p>You entered {$_SERVER['PHP_AUTH_PW']} as your password.</p>”;
}
?>

PHP:Validate URL (FTP, HTTP) using regular expression ( regex )

Monday, June 30th, 2008

<?php
/**
* valid_url - validates supplied url with a regular expression ( regex ).
*
* URL can be FTP, HTTP secure
* @param $url String
* @return true if the address is valid
* @author RAJI
*
*/
function valid_url( $url )
{
if ( !preg_match( ‘!^((ht|f)tps?://)?[a-zA-Z]{1}([w-]+.)+([w]{2,5})/?$!i’, $url ) )
{
return false;
} else
{
return true;
}
}
if ( valid_url( “http://blog.tryangled.com” ) )
{
echo “URL OK”;
} else
{
echo “Invalid URL.”;
}
?>

PHP:Force a secure HTTP connection

Saturday, June 28th, 2008

if (!($HTTPS == “on”)) {
header (”Location: https://$SERVER_NAME$php_SELF”);
exit;