Reputation: 2780
So I'm busy on this new site, and since it includes a login system, and my designer doesn't know any PHP at all, I decided to make a quick system for him to use.
When he needs to echo user data, he takes the attribute and puts it between percentage signs (For example %username%
. Then my class filters the document for those tags, and replaces them with the value I give them. So it works like this in the PHP class:
public static function setUser($key, $value)
{
self::$user['%' . $key . '%'] = $value;
}
As you can see, I call this function with a key and a value, and it gets saved in an array, then, I call the parseParams
function, which searches the page for these tags. The function:
private static function parseParams($content)
{
$userkeys = array_keys(self::$user);
$uservalues = array_values(self::$user);
self::$output = str_replace($userkeys, $uservalues, $content);
return self::$output;
}
The output is a page, since I call the render
function. It does a ob_get_contents
on the page I call, which then becomes the $content variable that gets parsed. When I echo the $output
variable, I get the whole page.
Seems pretty simple. Now, my problem is that there aren't any variables set. An example of when I call the function, is when a user logs in. Here's the login function:
public function Login()
{
$username = $_POST['username'];
$password = $_POST['password'];
$query = DB::$conn->prepare('SELECT username, password, rank FROM users WHERE username = ?');
$query->bind_param('s', $username);
$query->execute();
$query->bind_result($name, $pass, $rank);
$query->store_result();
if ($query->num_rows() > 0)
{
while ($query->fetch())
{
if (password_verify($password, $pass))
{
TPL::setUser('username', $username);
session_start();
define('LOGGEDIN', true);
header('Location: me');
exit();
}
else
{
$this->loginMessage[] = '{{wrongpass}}';
}
}
}
else
{
$this->loginMessage[] = '{{nouser}}';
}
}
I did define the TPL
class at the top of the file, under the namespace for the class:
use CRF\Template as TPL;
So, I was thinking this was a good solution, and it would work, but as soon as I do var_dump(TPL::$user);
I get an empty array.
Where does my mistake lay?
Thanks.
Upvotes: 1
Views: 72
Reputation: 1600
TPL::setUser('username', $username);
session_start();
$_SESSION['tpl_user']=serialize(self::$user);
define('LOGGEDIN', true);//btw this also won't be saved for the next script
header('Location: me');
exit();
you can not pass variables from one page to another that way. But you can store them in your $_SESSION var, something like this in file with logging in:
TPL::setUser('username', $username);
session_start();
$_SESSION['tpl_user']=serialize(self::$user);
define('LOGGEDIN', true);//btw this also won't be saved for the next script
header('Location: me');
exit();
and this in your file that uses that array:
private static function parseParams($content)
{
if(isset($_SESSION['tpl_user'])&&!empty($_SESSION['tpl_user'])) //session_start() should be called before that code
{
self::$user = unserialize($_SESSION['tpl_user']);
$userkeys = array_keys(self::$user);
$uservalues = array_values(self::$user);
self::$output = str_replace($userkeys, $uservalues, $content);
return self::$output;
} else {println("Access fobidden"); exit;}
}
Upvotes: 2