Hi all,
I’m very new to php and just started learning it. I am trying to make a website that is going to have different groups of users. I have a mysql database right now that stores the users’ username, password, and training_group. I would like to be able to redirect the user once they log on, to their specific homepage that is going to depend on which training_group they have been assigned to (I will be directly adding in their information into the database.) I have the website working to a point where I can login but now, I just need to be able to figure out where / how to implement the code to redirect people based on their training_group. Any help is greatly appreciated!
Right now I have a file, login.php:
[php]
<?php
session_start();
error_reporting(0);
require'database/connect.php';
require'functions/general.php';
require'functions/users.php';
if (logged_in() === true) {
$session_user_id =$_SESSION ['user_id'];
$user_data = user_data($session_user_id, 'user_id', 'username', 'password', 'first_name', 'last_name', 'email', 'class', 'training_group', 'week');
}
$errors = array();
if (empty($_POST) === false) {
$username = $_POST['username'];
$password = $_POST['password'];
if (empty($username) === true || empty($password) === true) {
$errors[] = 'please input a username and password first! ';
} else if (user_exists($username) === false) {
$errors[] = 'We could not locate you in our database!';
}
$login = login($username, $password);
if ($login === false) {
$errors [] = 'That username/password combination is incorrect';
} else {
$_SESSION['user_id'] = $login;
header('Location:logged_in/templates/logged_in_home.php');
exit ();
}
}
else {
$errors [] = 'No data received';
}
include 'includes/overall/header.php';
if (empty ($errors) === false) {
?>
<h2>We tried to log you in, but...</h2>
<?php echo output_errors($errors);
}
include 'includes/overall/footer.php';
?>
[/php]
a file users.php:
[php]
<?php
function user_data($user_id) {
$data = array();
$user_id = (int)$user_id;
$func_num_args = func_num_args();
$func_get_args = func_get_args();
if ($func_num_args > 1) {
unset($func_get_args[0]);
$fields = '`' . implode ('`,`',$func_get_args) . '`';
$data = mysql_fetch_assoc(mysql_query("SELECT $fields FROM `users` WHERE `user_id` = $user_id"));
return $data;
}
}
function logged_in() {
return(isset($_SESSION['user_id'])) ? true : false;
}
function user_exists($username) {
$username = sanitize($username);
return (mysql_result($query = mysql_query("SELECT COUNT(`user_id`) FROM `users` WHERE `username` = '$username'"), 0) == 1) ? true : false;
}
function user_id_from_username($username) {
$username = sanitize($username);
return mysql_result(mysql_query("SELECT `user_id` FROM `users` WHERE `username` = '$username'"), 0, 'user_id');
}
function login ($username,$password) {
$user_id = user_id_from_username($username);
$username = sanitize($username);
$password = md5($password);
return (mysql_result(mysql_query("SELECT COUNT(`user_id`) FROM `users` WHERE `username` = '$username' AND `password` = '$password'"),0) == 1) ? $user_id : false;
}
?>
[/php]