Hi all,
I've been playing around with PHP for a while now and never really studied hardcore (Just used it as best as I could on a whim) but I've now decided to sharpen the skill and get much better. I'm playing around with my first class and would like some feedback on its structure and general syntax. Is it a fairly good start? Are there any concerning issues I should be aware of? Thanks in advance! I've commented throughout so people are aware of what they're supposedly looking at.
<?php
class action{
////////////////////////////////////////////////////////
private function connect(){
$mysqli = mysqli_connect($config['host'], $config['user'], $host['password'], $config['db']);
if (mysqli_connect_errno()) {
printf("Connect failed: %s\n", mysqli_connect_error());
exit();
}
}
////////////////////////////////////////////////////////
private function disconnect(){
$thread = $mysqli->thread_id;
$mysqli->close();
$mysqli->kill($thread);
}
////////////////////////////////////////////////////////
public function userInfo(){
// Make the userInfo array available on a global scope.
global $userInfo;
//Lets make sure we are connected to the SQL DB
if ($mysqli_connection->connect_error) {
action::connect();
}
//Lets be awesome and protect ourself from SQL injections
$stmt = $mysqli->prepare("SELECT * FROM `users` WHERE `authuser` = ?");
$stmt->bind_param('s', $_SESSION['auth']);
$stmt->execute();
$stmt->store_result();
//This will allow us to use the array anywhere such as: $userInfo['authuser'];
$userInfo = $stmt->fetch_array(MYSQLI_ASSOC);
// Free up resources
$stmt->free();
//We need to keep the system optimised by closing our connections!
action::disconnect();
}
////////////////////////////////////////////////////////
public function checkAuth(){
// Do we have the users firstname? We should - but it's best to check
if(isset($_SESSION['auth'])){
if ((basename($_SERVER['PHP_SELF']) != "profile.php") && $userInfo['fn'] == null){
header('Location: profile.php');
exit();
}
// Stop logged in users from viewing login.php
if (basename($_SERVER['PHP_SELF']) == "login.php"){
header('Location: index.php');
exit();
}
}
// Okay, this user is not authorised to enter the platform.
// Lets make sure they're not accessing premium content.
else if (basename($_SERVER['PHP_SELF']) != "login.php"){
header('Location: login.php');
}
}
////////////////////////////////////////////////////////
}
?>
What I'm ultimately looking to do is improve on my PHP; thanks!