Procedural to OOP


Posted on 16th Feb 2014 07:03 pm by admin

ohn 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: [Select]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: [Select]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..

No comments posted yet

Your Answer:

Login to answer
123 Like 36 Dislike
Previous forums Next forums
Other forums

paginate search result
Hi, I have a paginations script to display data from my database but i would like to paginate someon

Array question
Does anyone know how to create a program that takes the $ amount for 8 different departments and gi

php wont update my db
hello,

sorry for posting in mysql forum but i dont know where exactly is the problem but here

Losing 'page' data
I have this code that allows me to update my database. But after updating, I lose the $_GET['page']

switch not getting value
what am i doing wrong?
i want to populate the country list according to the category
i want to

Not "Just Another" MMORPG - text based.
Hello,

I've been learning MySQL and PHP for the last 2-3 years.
Though, I've had long paus

header redirect problems? please help
Ok i am making a site that generates youtube thumbnails and i am generating an image that displays 1

LSB (PHP 5.3) problem with static value!
hello,

i'm having a problem. static::$text variable gets lost at some point. can someone plea

Auto-populating dropdowns and multiple forms.
Here's what I have so far:

First drop down = select a state (works)
This populates the sec

Help: problem with Headers to download PDF file
Hi,

I'm trying to implement a script where when a user clicks a link he gets a PDF file to op

Sign up to write
Sign up now if you have flare of writing..
Login   |   Register
Follow Us
Indyaspeak @ Facebook Indyaspeak @ Twitter Indyaspeak @ Pinterest RSS



Play Free Quiz and Win Cash