John Kleijn said that to avoid writing "crappy code", we should learn OOP and common OO principles. I've started reading OOP tutorials including the design patterns. And John is right, this is not an easy task. It's a shame because I'm finding it hard to absorb the tutorials.
Is it ok if I'll show you some of my procedural codes and you'll show me how to code some of its parts in OOP? I learn faster by playing with relevant codes. I hope it's ok with you guys.
Basically, all my php files look like this:
Code: ini_set('display_errors', 1);
error_reporting(E_ALL & ~E_NOTICE);
include("fxns.php");
define('TITLE', 'Page Title');
session_start();
if(isset($_SESSION['user_id'])) {
if (validate_user() == true) {
print_header(TITLE); // echos html codes
print_navs(); // echos html codes
//contents here
// calling more functions here
print_footer();
}
} else {
header('Location: login.php');
}
and my fxns.php would look like this:
Code: include("connect.php");
function validate_user() {
$sql = "SELECT uo.ip_address, uo.session_id, uo.user_agent, uo.activity
FROM users_online uo, users u
WHERE uo.user_id='$_SESSION[user_id]' AND uo.user_id=u.user_id";
$res = mysql_query($sql); $row = mysql_fetch_array($res);
$ip_address = $row[0];
$session_id = $row[1];
$user_agent = $row[2];
$activity = $row[3];
$res2 = mysql_query("SELECT config_value FROM config WHERE config_name='inactivity'");
$row2 = mysql_fetch_array($res2); $inactivity = $row2[0];
$res3 = mysql_query("SELECT locked, invalid_ip FROM users WHERE user_id='{$_SESSION[user_id]}'");
$row3 = mysql_fetch_array($res3); $locked = $row3[0]; $invalid_ip = $row3[1];
// verify user's IP address
if($ip_address != $_SERVER['REMOTE_ADDR']) {
show_invalid_ip($_SERVER['REMOTE_ADDR']); // echo some HTML
}
// verify if the the session id and user's session id are the same
elseif ($session_id != session_id()) {
show_invalid_sid(); // echo some HTML
}
// verify if the browser software is the same
elseif($user_agent != $_SERVER['HTTP_USER_AGENT']) {
show_invalid_user_agent(); // echo some HTML
}
// verify if the user is locked
elseif ($locked == 1) {
show_locked($invalid_ip); // echo some HTML
}
// verify if the user has been inactive for $inactivity mins
elseif( time() > ( $activity + ($inactivity * 60) ) ) {
show_s_expired($inactivity); // echo some HTML
} else {
update_user();
return true;
}
}
function update_user() {
$maxtime = time() - 1800;
mysql_query("UPDATE users_online SET refurl='{$_SERVER['HTTP_REFERER']}' WHERE user_id='$_SESSION[user_id]'");
mysql_query("UPDATE users_online SET activity='".time()."' WHERE user_id='$_SESSION[user_id]'");
mysql_query("DELETE FROM users_online WHERE activity < '$maxtime'");
}
Would you be kind to write some of its part in OOP? Thanks so much.
Packet Design
Hi All,Im new about network programming and I want to design a protocol but I have some questions about packet desing. For example I wanna do a sturct to send informations like that ;struct info {
Using system() and bringing back the results
I am aware that you can use system() within PHP to execute system commands, but I was wondering if there was a reasonable way to bring back the results every second.For example, I want a web based
How to calculate days from variable date?
This will be easy for one of you gurus. I want to fetch the date from a variable date, for example:Code: <?php$mydate = "";$daysaway = 400;$startdate = "5/5/2005";//I need
PHP5 - Verifying a secure mail is secure
I need to send an e-mail from a form to a external department and because it contains personal customer information, it must be secure.
Xacute search within SQL results
I have a query that is pulling data, and I want to return a specific value from the results of that query, so I'm using the following to do this:
Must be a string? Huh, what? HELP?
with the following script, I get Fatal error: Property name must be a string in /home/content/e/s/o/esone/html/test/123.php on line 24Anyone maybe know what is causing it?Code: <?phpfunction
FFmpeg Images 2 Video conversion problem ??
I got folder full of images , number of images = 600 , I want to make a video which plays one image per second hence duration of that video gona be 600 sec i.e 10 mins . (frame rate 1per sec) So to do
All possible combinations of Strings from table in PL/SQL
Hi,
include
Hi I have this way of licensing my PHP application, and I want to know if you guys can answer some questions with it.So there is the index.php on the server of the person who bought it
Baffled by Undefined Index in Simple Array: Please Help!
Hello. I have a form which posts an array to this script. However, I can't seem to access the values in the array; I keep getting an undefined index. Something is not right, but the code is so simple!