It's been like a year i haven't code strictly using OOP, only pieces... pieces of scripts lol..., and kinda now learning pdo, i just want you guys to tell me if i'm coding well or not before i continue
class.core.php
index.php
class.core.php
PHP Code:
<?php
class core extends config
{
function status()
{
$config = new config();
$dbhost = $config->dbhost;
$dbname = $config->dbname;
$dbuser = $config->dbuser;
$dbpass = $config->dbpass;
$pdo = new PDO("mysql:host=$dbhost;dbname=$dbname",$dbuser,$dbpass, array(PDO::ATTR_PERSISTENT => true));
$status = htmlspecialchars($_POST["status"],ENT_QUOTES);
$user = $_SESSION["username"];
$query = $pdo->prepare("SELECT * FROM status WHERE user='$user'");
$query->execute();
if($query->rowCount()=="0")
{
$query = $pdo->prepare("INSERT INTO status VALUES ('$user', '$status')");
$query->execute();
}
else
{
$query = $pdo->prepare("UPDATE status SET status='$status' WHERE user='$user'");
$query->execute();
}
header("Location: index.php?status_updated");
exit;
}
function main_page()
{
$config = new config();
$dbhost = $config->dbhost;
$dbname = $config->dbname;
$dbuser = $config->dbuser;
$dbpass = $config->dbpass;
$pdo = new PDO("mysql:host=$dbhost;dbname=$dbname",$dbuser,$dbpass, array(PDO::ATTR_PERSISTENT => true));
$site_description = $config->site_description;
echo '<br/><br/><div class="logo">
<span class="red">A</span>
<span class="lime">I</span>
<span class="blue">O</span>
<span class="black"> CHAT</span>
<span class="red">!</span>
</div><br/><hr/><br/><h1>'.$site_description.'</h1><br/>';
if(isset($_GET["logged_out"])) { echo '<div class="center"><span class="red">You have been logged out.</span></div><br/>'; }
echo '<hr/><br/><div class="center">
<img src="img/Checkmark.png" alt="Checkmark"/> Login</div>
<br/><form action="index.php?action=login" method="post">
<div class="center">Username: <span class="red">*</span>
<input type="text" maxlength="20" size="15" name="username"/><br/><br/>
Password: <span class="red">*</span>
<input type="password" maxlength="25" name="password" size="15"/><br/><br/>
<input type="submit" value="Login..."/></div></form><br/><br/>';
?><div class="center">
Don't have an account?
<?php
echo '<br/><br/><img src="img/Add_symbol.png" alt="register"/> <a href="index.php?action=register">Register Now</a>';
echo '<br/><br/><hr/><br/>Meet New Friends<br/><br/>';
$query = $pdo->prepare("SELECT * FROM photos ORDER BY rand() LIMIT 2");
$query->execute();
foreach($query as $row)
{
echo ' <a href="index.php?action=profile&user='.$row["user"].'"><img class="shadow" src="thumb.php?path=upload/'.$row["name"].'" alt="Meet New Friends"/></a>';
}
echo '<br/>';
$query = $pdo->prepare("SELECT * FROM photos ORDER BY rand() LIMIT 2");
$query->execute();
foreach($query as $row)
{
echo ' <a href="index.php?action=profile&user='.$row["user"].'"><img class="shadow" src="thumb.php?path=upload/'.$row["name"].'" alt="Meet New Friends"/></a>';
}
echo '<hr/><br/><br/>
<div class="big">Features</div><div class="center">
<ul><li>Mobile Chat Rooms</li>
<li><a href="smileys.php">Awesome smileys</a></li>
<li>Notifications</li>
<li>Private messaging</li>
<li>Shoutbox</li>
<li><a href="games.php">AI chatbot / games</a></li>
<li>Photo Albums</li><li><a href="forum.php">Forums / Discussion</a></li>
<li>Custom profile layouts - colors, videos etc...</li>
<li>and many more...</li>
</ul></div><hr/>
<br/><br/><div class="big">Users Registered [<a href="index.php?action=all_users"><span class="red">';
$query = $pdo->prepare("SELECT * FROM members");
$query->execute();
echo $query->rowCount();
echo '</span></a>]</div>';
echo '</div><br/><br/>';
}
function user_logged()
{
$config = new config();
$dbhost = $config->dbhost;
$dbname = $config->dbname;
$dbuser = $config->dbuser;
$dbpass = $config->dbpass;
$pdo = new PDO("mysql:host=$dbhost;dbname=$dbname",$dbuser,$dbpass, array(PDO::ATTR_PERSISTENT => true));
$user = htmlspecialchars($_SESSION["username"],ENT_QUOTES);
echo '<br/><div class="bold"><img src="img/User2.png" alt="User"/>Logged in as '.$user.'!</div><br/>';
echo '<div class="center">'.this_time().'</div><br/>';
echo '<div class="center">';
echo "What's on your mind?<br/><br/>";
$query = $pdo->prepare("SELECT * FROM status WHERE user='$user'");
$query->execute();
if($query->rowCount()>0)
{
$row = $query->fetch(PDO::FETCH_ASSOC);
$status = $row["status"];
}
else
{
$status = '';
}
if(isset($_GET["status_updated"])) { echo '<div class="center"><span class="red">Status Updated!</span></div><br/>'; }
echo '<form action="index.php?action=status" method="post"><div class="center"><input type="text" name="status" value="'.$status.'" maxlength="250"/>
<br/><br/><input type="submit" value="Update!"/></div></form>';
echo '<br/>Notifications! [<span class="red">';
$query = $pdo->prepare("SELECT * FROM notifications WHERE user='$user'");
$query->execute();
echo $query->rowCount();
echo '</span>]<br/><br/>';
echo '</div>';
echo '<div class="bold"><a href="index.php?action=logout"><img src="img/Minus.png" alt="Log Out"/><br/>Log out</a></div><br/>';
}
function logout()
{
session_destroy();
header("Location: index.php?logged_out");
exit;
}
function register_new()
{
}
function login()
{
if(empty($_POST["username"]) && empty($_POST["password"]))
{
echo '<br/><div class="bold">Login!</div><br/><br/>
<form action="index.php?action=login" method="post">
<div class="center">Username: <span class="red">*</span>
<input type="text" maxlength="20" size="15" name="username"/><br/><br/>
Password: <span class="red">*</span>
<input type="password" maxlength="25" name="password" size="15"/><br/><br/>
<input type="submit" value="Login..."/></div></form><br/>';
}
else
{
$username = htmlspecialchars($_POST["username"], ENT_QUOTES);
$config = new config();
$dbhost = $config->dbhost;
$dbname = $config->dbname;
$dbuser = $config->dbuser;
$dbpass = $config->dbpass;
$pdo = new PDO("mysql:host=$dbhost;dbname=$dbname",$dbuser,$dbpass, array(PDO::ATTR_PERSISTENT => true));
$query = $pdo->prepare("SELECT * FROM members WHERE username='$username'");
$query->execute();
$row = $query->fetch(PDO::FETCH_ASSOC);
$key = $row["password"];
$password = md5($_POST["password"]);
if($password===$key)
{
$_SESSION["username"] = $username;
header("Location: index.php");
exit;
}
else
{
echo '
<br/><div class="bold">Login!</div><br/>
<div class="error"><img src="img/Warning_2.png" alt="Error"/> Incorrect Username and/or Password!</div><br/>
<form action="index.php?action=login" method="post">
<div class="center">Username: <span class="red">*</span>
<input type="text" maxlength="20" size="15" name="username"/><br/><br/>
Password: <span class="red">*</span>
<input type="password" maxlength="25" name="password" size="15"/><br/><br/>
<input type="submit" value="Login..."/></div></form><br/>
';
}
}
}
function online($list)
{
require_once('class/class.pagination.php');
$config = new config();
$dbhost = $config->dbhost;
$dbname = $config->dbname;
$dbuser = $config->dbuser;
$dbpass = $config->dbpass;
switch($list)
{
case "all_users":
echo '<div class="center">';
try
{
$dbh = new PDO("mysql:host=$dbhost;dbname=$dbname",$dbuser,$dbpass, array(PDO::ATTR_PERSISTENT => true));
}
catch(PDOException $e)
{
print "Error!: " . $e->getMessage() . "<br/>";
die();
}
if(isset($_GET['page']))
{
$page = $_GET['page'];
}
else
{
$page = 1;
}
$options = array(
'results_per_page' => 20,
'url' => 'index.php?action=all_users&page=*VAR*',
'db_handle' => $dbh
);
try
{
$paginate = new pagination($page, 'SELECT * FROM members', $options);
}
catch(paginationException $e)
{
echo $e;
exit();
}
if($paginate->success == true)
{
$result = $paginate->resultset->fetchAll();
echo '<br/><div class="bold">All users: <span class="red">['.$paginate->total_results.']</span></div><br/><br/>';
foreach($result as $row)
{
echo '<a href="index.php?action=profile&user='.$row["username"].'">'.$row["username"].'</a><br/><br/>';
}
echo $paginate->links_html;
}
echo '</div><br/>';
break;
}
}
function profile($profile)
{
$config = new config();
$dbhost = $config->dbhost;
$dbname = $config->dbname;
$dbuser = $config->dbuser;
$dbpass = $config->dbpass;
$pdo = new PDO("mysql:host=$dbhost;dbname=$dbname",$dbuser,$dbpass, array(PDO::ATTR_PERSISTENT => true));
echo "<br/><div class=\"bold\">$profile's Proflie</div><br/>
<div class=\"center\">";
$query = $pdo->prepare("SELECT * FROM online WHERE user='$profile'");
$query->execute();
if($query->rowCount()>0)
{
echo '<div class="big"><span class="lime"><img src="img/Valid.png" alt="Online"/> Online!</span></div><br/>';
}
else
{
echo '<div class="big"><span class="red"><img src="img/Warning_2.png" alt="Offline"/> Offline!</span></div><br/>';
}
$query = $pdo->prepare("SELECT * FROM propic WHERE user='$profile' LIMIT 1");
$query->execute();
if($query->rowCount()>0)
{
foreach($query as $row)
{
echo '<img class="propic" src="upload/'.$row["image"].'" alt="'.$profile.'"/>';
}
}
else
{
echo '<img class="propic" src="img/nophoto.png" alt="'.$profile.'"/><br/><br/>'.$profile.' has not chosen a profile picture.';
}
echo '<br/><br/>My Friends ';
$query = $pdo->prepare("SELECT * FROM friends WHERE user='$profile'");
$query->execute();
echo '[<span class="red">'.$query->rowCount().'</span>]';
echo '<br/><br/> My Photos ';
$query = $pdo->prepare("SELECT * FROM photos WHERE user='$profile'");
$query->execute();
echo '[<span class="red">'.$query->rowCount().'</span>]';
echo '<br/><br/><img src="img/Calendar.png" alt="Calendar"/><br/>Date of Birth<br/>';
$query = $pdo->prepare("SELECT * FROM members WHERE username='$profile'");
$query->execute();
foreach($query as $row)
{
echo $row["date"];
}
echo '<br/><br/><img src="img/sex.png" alt="Gender"/><br/>Gender<br/>';
$query = $pdo->prepare("SELECT * FROM members WHERE username='$profile'");
$query->execute();
foreach($query as $row)
{
echo $row["gender"];
}
echo '<br/><br/><img src="img/Location.png" alt="Location"/><br/>Location<br/>';
$query = $pdo->prepare("SELECT * FROM members WHERE username='$profile'");
$query->execute();
foreach($query as $row)
{
echo $row["country"];
}
echo '<br/><br/><img src="img/About.png" alt="About"/><br/>About Me<br/>';
$query = $pdo->prepare("SELECT * FROM members WHERE username='$profile'");
$query->execute();
foreach($query as $row)
{
echo $row["about"];
}
echo '<br/><br/>';
echo '</div>';
}
}
?>
PHP Code:
<?php
include("class/class.config.php");
include("class/class.core.php");
include("include/header.php");
$config = new config();
$action = new core();
$dbhost = $config->dbhost;
$dbname = $config->dbname;
$dbuser = $config->dbuser;
$dbpass = $config->dbpass;
$pdo = new PDO("mysql:host=$dbhost;dbname=$dbname",$dbuser,$dbpass, array(PDO::ATTR_PERSISTENT => true));
if(!empty($_GET["action"]))
{
$show = htmlspecialchars($_GET["action"],ENT_QUOTES);
}
else
{
if(!empty($_SESSION["username"]))
{ $show = 'user_logged'; } else { $show = 'main_page'; }
}
switch($show)
{
case "login":
$action->login();
break;
case "status":
$action->status();
break;
case "logout":
$action->logout();
break;
case "all_users":
$action->online("all_users");
break;
case "profile":
$profile = htmlspecialchars($_GET["user"],ENT_QUOTES);
$action->profile($profile);
break;
case "register_new":
$action->register_new();
break;
case "main_page":
$action->main_page();
break;
case "user_logged":
$action->user_logged();
break;
}
include("include/footer.php");
?>
Comment