We have recently moved to a different host and the website is now set up on a dual servers, and we are experiencing issues where users are logged out after seconds, so I looked into it and came across memcache, which the hosts have now told me is set up with the library.
They then asked me to set my bit up, so I looked into and found a tutorial where I create a file called cache.php and add the following code -
<?php
# Copyright 2012 John Post - StarfixIT
# NOTE: Requires PHP version 5.3 or later with Memcache module
class cache
{
private $host = '127.0.0.1';
private $port = '11211';
private $memcache;
private $lifetime;
private $name;
function __construct($name = false, $lifetime = 600)
{
//Check if Memcache is installed
if(!class_exists("Memcache")){
exit("You need to install memcache");
}
//Check if allready connected to memcached
if (isset($_GLOBALS["memcache"])) {
//Yes, use old connection
$this->memcache = $_GLOBALS["memcache"];
} else {
//No, make new connection
$this->memcache = new Memcache;
$this->memcache->connect($this->host, $this->port);
$_GLOBALS["memcache"] = $this->memcache;
}
//Set global livetime
$this->lifetime = $lifetime;
//Set global name
if ($name !== false) {
$this->name = $this->makeNameOk($name);
}
}
//Public function to set the name
public function setName($name)
{
$this->name = $this->makeNameOk($name);
}
//Public function to set the cache, livetime optional
public function setCache($data, $lifetime = false)
{
$lifetime = $lifetime === false ? $this->lifetime : $this->lifetime;
$this->memcache->set($this->name, $data, MEMCACHE_COMPRESSED, $lifetime);
}
//Public function to get the cache, optional name
public function getCache($name = false)
{
$name = $name === false ? $this->name : $name;
$result = $this->memcache->get($name);
if ($result !== false) {
return $result;
}
return false;
}
//Public function to remove the data from Memcached
public function remove( $name = false )
{
$name = $name === false ? $this->name : $name;
$this->memcache->delete( $name );
}
//Function to create a clean alias
private function makeNameOk($name)
{
return preg_replace('/[^A-Za-z0-9_]/', "", $name);
}
}
And then it says to include that on my pages where I need it, but the trouble is I have already got a way of creating a session as below on logging in
$username=$_POST['txtuser'];
$password=md5($_POST['txtpass']);
if ($username==$data['Username'] && $password==$data['Password'])
{
$error1="correct";
$_SESSION['user1']=$username;
$_SESSION['flaglog']=1;
Then on each page after that, I have
$username=$_SESSION['user1'];
if ($_SESSION['flaglog']!=1)
{
header("location:index.php");
}
else
{
$query = "SELECT * FROM Users WHERE Username='$username'";
$res = sqlsrv_query($conn, $query);
while ($result = sqlsrv_fetch_array($res, SQLSRV_FETCH_ASSOC)) {
$name = $result["First_Name"];
$security = $result["Admin"];
//echo $security;
}
So where I’m baffled is how to get memcache working with the way I’m set up at the moment.