19 March, 2011

Secure a page using php

By default php script runs on port 80 unless changed, but i had to secure a page so that it could run only on secure port i.e. 443. So what i did was used a code to force to run a page on secure port i.e. https which uses port 443, Here's what i did:

$serverPort = $_SERVER['HTTP_PORT'];
//check if its not secure port
if("443" != $serverPort) {
header("Location: https://".$_SERVER["HTTP_HOST"] . $_SERVER["REQUEST_URI"]);
exit;
}
?>

So, the above code ensures that the page runs under https i.e. port 443 and
is securely displayed. Hope it helps someone.

3 comments:

Anonymous said...

Hey this is lovely article. Your all posts are unique. Nice, really you are genius guy.

pokershark103 said...

How I can use it with php forms

Sudhir Bastakoti said...

Hi,
You can use it at top of the page.