Saturday, November 14, 2015

Advance guest views counter


  • Create 32 bit hash using php.
  $randomString = substr(str_shuffle("0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ"), 0, 32);
  • Advance Guest views counter with month valid cookie.
setcookie($guest, $randomString, time() + (30*86400), "/"); // 86400 = 1 day
  • Full Example.

<?php
$guest = "Guest";
if(!isset($_COOKIE[$guest])) {
$guestid = "";
    echo "Cookie named '" . $guest . "' is not set!";
    $randomString = substr(str_shuffle("0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ"), 0, 32);
setcookie($guest, $randomString, time() + (30*86400), "/"); // 86400 = 1 day

if ($randomString != "") {
$sql = "INSERT INTO guest (hash, views) VALUES ('$randomString', '+1')";
if ($con->query($sql) === TRUE) { echo "You Can Enjoy Site As Guest.Some Features Maybe Disable for Unregistered Users."; } else { echo "Error: " . $sql . "<br>" . $conn->error; }
}
} else {
    echo "Cookie '" . $guest . "' is set!<br>";
    echo "Value is: " . $_COOKIE[$guest];
  $sql = "UPDATE guest SET views=views+1 , lastvisit=Now() WHERE hash='$_COOKIE[$guest]'";
if ($con->query($sql) === TRUE) { echo "You Can Enjoying Site As Guest.Some Features Maybe Disable for Unregistered Users."; } else { echo "Error: " . $sql . "<br>" . $con->error; }
$result = mysqli_query($con,"SELECT * FROM `guest` WHERE `hash` LIKE '$_COOKIE[$guest]'");
while($check = mysqli_fetch_array($result)) {
$guestid = $check['id'];
echo "Guest id is" . $guestid;
}
?>
  • Database.

mysql sum() funtion


  • SUM() Function Use To Get Total Sum Of a Numeric Column.
  • Example Code.
<?php require 'DB_conn.php'; ?>
<?php
$result = mysqli_query($con,"SELECT SUM(views) AS views FROM guest;");
$user_count = 1;
while($check = mysqli_fetch_array($result)) {
$offline_views = $check['views'];
$user_count++;
}
echo $offline_views . "<br>";
echo $ofline_user_count;

?>

  • Database.
  • Result given.
9
7