functions.php
[php]<?php
/* Change ‘Asia/Karachi’ according to your timezone. */
date_default_timezone_set(‘Asia/Karachi’);
function db_connect(){
/* Replace the values of
localhost
username
password
database
with your own details */
$host = 'localhost';
$user = 'tanzeelniazi';
$pass = 'abc';
$db = 'phphelp';
$conn = @mysqli_connect($host, $user, $pass, $db);
if (mysqli_connect_errno()) { die ("Can't Connect to Database");}
return $conn;
}
/* Function to strip out bad things users enter via forms */
function safe_output($form){
$form = strip_tags($form);
$form = trim($form);
$form = htmlspecialchars($form);
return $form;
}
/*
Function to secure user’s password using BlowFish Hashing Algorithm
If you are using a version prior to PHP 5.3 then It will not work, therefore you need to change the hashing algorithm.
*/
function secure_password($conn, $password){
$hash_format = "$2y$10$";
$salt = md5("Tanz33lN!@zi@SpinZRphp");
$salt_format = $hash_format . $salt;
$hash = crypt($password, $salt_format);
return $hash;
}
/*
This function will check usernames and email addresses in the database. If any existing record is found it will return a string otherwise it will return NULL if no record is found.
*/
function query_check_user($conn, $username, $email ){
$query = "SELECT * FROM users
WHERE username = '{$username}'
";
$results = mysqli_query($conn, $query);
if ($results && mysqli_affected_rows($conn) == 1){
$existing_user = "Username Already Exists, choose another one";
return $existing_user;
}
$query = "SELECT * FROM users
WHERE email = '{$email}'
";
$results = mysqli_query($conn, $query);
if ($results && mysqli_affected_rows($conn) == 1){
$existing_user = "Email Already Exists, choose another one";
return $existing_user;
}
return NULL;
}
/*
Once the ‘query_check_user’ function returns NULL. This function will add a new user to the database and will return TRUE if sucessful.
*/
function query_add_user($conn, $name, $username,
$email, $pass, $vericode = 0){
$query = "INSERT INTO
users
VALUES (
NULL,
'{$name}',
'{$username}',
'{$pass}',
'{$email}',
'unverified',
'{$vericode}'
)
";
$results = mysqli_query($conn, $query);
if (!$results) {
die(mysqli_error($conn));
}
return TRUE;
}
/*
Once the ‘query_add_user’ function succeeds. This function will select the newly added user and will return an array with variables likes name, username, email and vericode. So that it can be used to verify the email address by sending an email to the email address specified during registration.
*/
function query_verify_user($conn, $username, $email, $pass){
$query = "SELECT * FROM users
WHERE username = '{$username}' AND
password = '{$pass}' AND
email = '{$email}'
";
$results = mysqli_query($conn, $query);
if ($results && mysqli_affected_rows($conn) == 1){
while ($user = mysqli_fetch_assoc($results)){
return $user;
}
} else { return NULL; }
}
/*
When the user click on the link sent to them via email. This function will update thier status from Unverified to Verified.
*/
function query_update_user($conn, $email, $vericode){
$query = "UPDATE users
SET
status = 'verified'
WHERE
email = '{$email}' AND
vericode = '{$vericode}'
";
$results = mysqli_query($conn, $query);
if ($results && mysqli_affected_rows($conn) == 1){
return $results;
}
else { return NULL; }
}
/*
This function will give administrator the ability to block a user from accessing their account and they will be unable to post new comments on the comments page.
Remember Users are not deleted permanently from the database but only their status will be changed from 'Verified' to 'Deleted'.
*/
function query_delete_user($conn, $user){
$query = "UPDATE users
SET
status = 'deleted'
WHERE
username = '{$user}'
";
$results = mysqli_query($conn, $query);
if (!$results){
die(mysqli_error($conn));
}
return NULL;
}
/*
This function will check for the users who are attempting to login.
If their status is 'Unverified' or 'Deleted' it will return a string to the function and they will not be able to login.
If login was successful a session variable 'username' will be created and they will be redirected to the comments page.
*/
function query_login_user($conn, $username, $pass){
$query = "SELECT * FROM users
WHERE username = '{$username}' AND
password = '{$pass}' AND
status = 'unverified'
";
$results = mysqli_query($conn, $query);
if ($results && mysqli_affected_rows($conn) == 1){
$acc_status = '<p>Your account is not yet verified,
please contact the site administrator.
</p>
';
return $acc_status;
}
$query = "SELECT * FROM users
WHERE username = '{$username}' AND
password = '{$pass}' AND
status = 'deleted'
";
$results = mysqli_query($conn, $query);
if ($results && mysqli_affected_rows($conn) == 1){
$acc_status = '<p>Your account has been blocked,
please contact the site administrator.
</p>
';
return $acc_status;
}
$query = "SELECT * FROM users
WHERE username = '{$username}' AND
password = '{$pass}' AND
status = 'verified'
";
$results = mysqli_query($conn, $query);
if ($results && mysqli_affected_rows($conn) == 1){
$_SESSION['username'] = $user['username'];
header("Location: comment.php");
} else { return NULL; }
}
/*
This function will give users the ability to post comments on the comments page. All comments will be ‘Unapproved’ by default except for the admin.
Then the admin has to manually approve all the comments posted by other users.
*/
function query_insert_comment($conn, $username, $comment, $time,
$status = ‘Unapproved’){
$query = "INSERT INTO comments
VALUES (NULL, '{$username}', '{$comment}', '{$time}', '{$status}'
)";
$results = mysqli_query($conn, $query);
if(!$results) {die();}
return mysqli_affected_rows($conn);
}
/*
This function will fetch all the comments posted on in the comments table so far and will return an array.
*/
function query_fetch_comments($conn){
$query = "SELECT * FROM comments ORDER BY id DESC";
$results = mysqli_query($conn, $query);
if(!$results) {die();}
while($comment = mysqli_fetch_assoc($results))
{
$id[] = $comment['id'];
$uname[] = $comment['username'];
$ucomment[] = $comment['comment'];
$ctime[] = $comment['comment_time'];
$status[] = $comment['status'];
}
if (mysqli_affected_rows($conn) > 0){
return $comments = array('id' => $id,
'uname' => $uname,
'ucomment' => $ucomment,
'ctime' => $ctime,
'status' => $status
);
}
}
/*
Gives admin the ability to delete comments.
*/
function query_delete_comment($conn, $id){
$query = "DELETE FROM comments WHERE id = {$id}";
$results = mysqli_query($conn, $query);
if(!$results) {die();}
return NULL;
}
/*
Gives admin the ability to approve / un-approve comments.
*/
function query_approve_comment($conn, $id, $status){
$query = "UPDATE comments
SET status = '{$status}'
WHERE id = {$id}";
$results = mysqli_query($conn, $query);
if(!$results) {die();}
return NULL;
}
/*
If a user’s status is set to ‘Deleted’ his comment will be set to ‘Unapproved’ and username for the comment will be set to ‘Anonymous’.
It will not affect all the previous comments of the users but only a particular comment.
*/
function query_anonymous_comment($conn, $username, $id){
$query = "UPDATE comments
SET
username = 'Anonymous',
status = 'Unapproved'
WHERE id = {$id}";
$results = mysqli_query($conn, $query);
if(!$results) {die();}
return NULL;
}
?>[/php]