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.
On page view, minus credit
Hello all, please, I need a little help with this script. I am charging one credit (credits can be purchased, but that is unrelated) for view of a certain page. I need help in figuring out what php I
extract a file from zip file
hi,i know how to unzip a zip file in php, but is there a way to just extract a certain file only?
if description does not contain the following words
Hello everyone,I am parsing a MS Excel (.xls) file and adding it's content to a mySQL database, so far to good, but what I want to do is if the column titled description does not contain these words
If statement help please.
Hello on my site I have 2 content boxes. 1 is for the main content (which is the biggest one) and the second one is for a sub navigation (which is a small box). I am trying to write an IF statement so
Sharing PHP Sessions Across Domains
I am in the process of writing a script to share a php session across various domains I have.The problem I have, is getting php to access the php session. It gives me a persmission error. It seems the
'210010106140040100' == '210010106140040101'
Debugging this simple line of a PHP scriptCode: if($a == $b){ } I've found that with value of$a = '210010106140040100' (type = string)$b = '210010106140040101' (type = string)the result of the
Multi language - seo links
Im trying to figure out the best method to have a multi language website that will function with seo friendly links.The code that ive used many times before.....Code: $_SESSION['lang'] = $lang =
Adding to an Int row in db
Hi, i have a database which houses all of the users of my site. One of the columns is for points which is default 0.What I would like to do is add 1 to this number each time they upload an image. What
Is this possible? If one file assigned to an ID...
I have a page that lists property listings assigned to a specific user (a manage page), with brief text details and a picture.Each of these listings has several images with it, that aren't seen unless
Big O & time complexity ???
can you help me how to calculate the Big O & time complexity for any algorithm and c++ program please?