Archive for the 'Codes' Category

How to Use Old Code with New Versions of PHP

Sunday, August 16th, 2009

as_code.gif

PHP developers try to maintain backwards compatibility in PHP programming such that a script written for an older version will still run without changes in a newer version. Two of the most important recent changes that affect old code are:

• The deprecation of the old $HTTP_*_VARS arrays which should be indicated as global when used inside a function or method. The following superglobal arrays were introduced in PHP » 4.1.0. They are: a) $_GET; b) $_POST; c) $_COOKIE; d) $_SERVER; e) $_FILES; f) $_ENV; g) $_REQUEST, and; h) $_SESSION. The older $HTTP_*_VARS arrays like $HTTP_POST_VARS are also available. In PHP 5.0.0, the long PHP predefined variable arrays may be disabled with the register_long_arrays directive.

• By default, external variables are no longer registered in the global scope, meaning in PHP » 4.2.0 the PHP directive register_globals is off by default in php.ini. These values can be accessed through the superglobal arrays.

Cookies: Monsters?

Monday, October 6th, 2008

Image Source:images.malwarehelp.org

HTTP or Hyper Text Transfer Protocol is basically stateless. It simply means that any data that you have made starts from scratch again when the page that you requested has been sent and the connection between them is terminated. Developers worked on, to find a way to solve the problem. The first to come up with the idea of storing small data on the client was Netscape. They eventually created the “cookies” as the solution to that problem. The Netscape browser came up with cookies. These are small bits of information that a particular website on the client’s computer. These will then be sent back by the PC to the website every time that particular page was requested. a cookie can olny be used by the website that wrote them. It is a secure way to store data between pages. Cookies made some negative headlines a few years back. Initially it was thought that these cookies were employed and installed in the PCs to stalk users’ confidential infos. And came to a point that it was encouraged to disable cookies when browsing. But in the end, people got the real deal about the cookies and why its there. They’re basically harmless.

Looping Statement At PHP

Sunday, August 10th, 2008


Image Source: www.ariel.web.id
Looping statements, like Do-While looping statements, While looping statements, and For looping statements, makes programming very easy, even at making PHPs. But did you know that there is something that looping statements can’t loop? That’s right! There is something. Looping statements can’t loop If-Else statements. That’s because when the looping statement starts, it goes through the If-Else statement, and then, whatever the statement the If-Else statement supplies, it will be the new statement the looping statement loops, and the If-Else statement will be left aside. I’ve been creating PHPs in our school and I tell you, it’s very frustrating that your looping statements are not looping the whole statements you put inside it. Thus, your website will not work properly. The only solution that I find is that you have to use Switch statements. Switch statements are like If-Else statements, but the Switch statements are more specific. Try it! It works, even at other programming language

.htaccess Password Protection

Friday, May 30th, 2008

securelock.jpg

Even without using mySQl, users can still password protect their files through .htaccess password protection.

The code can be divided into 3 if-else statements:

1. If the user has not been validated, use the PHP header and request for a username and password.

2. Else, if the user’s name is “webworld” and the password is “webworld”, log in. Here, all the code for the user will be placed.

3. Else say the user/password failed.

//1st part
if (!isset($PHP_AUTH_USER))
{
header("WWW-Authenticate: Basic realm=\"webworld Password.\"");
Header("HTTP/1.0 401 Unauthorized");
exit;
}

//2nd part
else if(($PHP_AUTH_USER=="webworld") && ($PHP_AUTH_PW=="webworld"))
{
echo "You are logged in...";
//put the code for the whole user page in here
//you can also create a redirect to the user page if you want
}

//3rd part
else
{
echo "Failed entry”;
//fail try again
}
?>

Remember that the username and password will remain for the whole session as long as explorer windows is open.

Displaying Page Loading Time (Steps and Sample Code)

Monday, May 26th, 2008

stopwatch.gif

Here is how to display your page’s loading time:

1. Use the function microtime() to get the time in micro-seconds
2. Use the explode() function to turn the micro-time into an array.
3. Combine the two parts to the array (the micro-seconds to the seconds).
4. Repeat steps 1,2 and 3 for the bottom of the page
5. Take the time taken at the end of the page from the time taken at the top of the page to determine the total loading time.
6. After rounding the microtime, return it to the browser.

At the top of your page, place:

$m_time = explode(" ",microtime());
$m_time = $m_time[0] + $m_time[1];
$starttime = $m_time;
?>

At the bottom of your page, place:

$round = 3;// The number of decimal places to round the micro time to.
$m_time = explode(" ",microtime());
$m_time = $m_time[0] + $m_time[1];
$endtime = $m_time;
$totaltime = ($endtime - $starttime);
echo "Page loading took:". round($totaltime,$round) ." seconds";
?>

Creating a Simple User Log (Sample Code)

Friday, May 23rd, 2008

wwwglobe.jpg

//use the date() function <----- comment
$time = date("F jS Y, h:iA");

//use PHP variable $remote_addr to get ip address
$ip = $REMOTE_ADDR;

//use PHP variable $http_referer to get referer
$referer = $HTTP_REFERER;

//user PHP variable $http_user_agent to get browser
$browser = $HTTP_USER_AGENT;

//what page they came from
$page = $_SERVER['REQUEST_URI'];

//use the fopen() function
$fp = fopen("log.html", "a");

//use the fputs() function
fputs($fp, "
Time: $time
IP: $ip
Referer: $referer
Browser: $browser
Page: $page
");

fclose($fp);

?>

Note: Don’t forget to create a blank log.html page, upload it to the server in the same directory as the page where the code will be placed, and CHMOD it to 777. Also, you can put this code anywhere on your main page.