Not redirecting to home page (index.php) - php

Is there something wrong about my code? It works but its not redirecting to my index.php it always ended up in the login.php where the form is located.
<?php
include 'core/ini.php';
if (empty($_POST) === false) {
$username = $_POST ['username'];
$password = $_POST ['password'];
if (empty ($username) === true || empty ($password) === true ) {
$errors[] = 'You need to enter a username and password!';
} else if (user_exists($username) === false) {
$errors[] = 'We can\'t find that username. Have you registered?';
} else if (user_active($username) === false) {
$errors[] = 'You haven\'t activated your account! ';
} else {
$login = login($username, $password) ;
if ($login === false) {
$errors[] = 'That username/password combination is incorrect ';
} else {
$_SESSION['user_id'] = $login;
header('Location :index.php');
exit();
}
}
print_r($errors);
}
?>
thanks!
EDIT *
this is my login.php
<?php
include 'core/ini.php';
if (empty($_POST) === false) {
$username = $_POST ['username'];
$password = $_POST ['password'];
if (empty ($username) === true || empty ($password) === true ) {
$errors[] = 'You need to enter a username and password!';
} else if (user_exists($username) === false) {
$errors[] = 'We can\'t find that username. Have you registered?';
} else if (user_active($username) === false) {
$errors[] = 'You haven\'t activated your account! ';
} else {
$login = login($username, $password) ;
if ($login === false) {
$errors[] = 'That username/password combination is incorrect ';
} else {
$_SESSION['user_id'] = $login;
header('Location :index.php');
exit();
}
}
print_r($errors);
}
?>
this is where the process go. I don't know where should I put my start session but I don't know why it works without having an error.

change header('Location :index.php'); to header('Location: index.php'); That space might be the cause.

I guess you missed the session_start(); on top of the page since you are storing session. Initiate the session_start();.
Also does your login() function returns TRUE? Echo something to check whether the function returns TRUE as expected.

You hae to use session_start on top of page and I think you should remove exit after headerlocation..

Related

header redirect function is not executing no errors are given

i am trying to redirect the user when the login details are correct, but my header function is not running, I have tested the if statement without the header function by echo something out and this worked, so im not sure why the header is not being executed. any help would be appreciated.
login php
<?php
session_start();
require '../includes/class/login.php';
$login = new login();
$username_error=' ';
$password_error=' ';
$result=' ';
if(isset($_POST['username']) && $_POST['username'] !== ""){
$username = $_POST['username'];
$username = trim($username);
$user_exists = $login->user_exists($username);
if ($user_exists) {
$username_error = 'correct username entered ';
} else {
$username_error = 'username not found';
}
}
if(isset($_POST['password']) && $_POST['password'] !== ""){
$password = $_POST['password'];
$password = trim($password);
$password_check = $login->password_check($username,$password);
if ($password_check) {
$password_error = 'correct password entered ';
} else {
$password_error = 'password not found';
}
}
if (isset($_POST['login'])&& $_POST['login'] !== "") {
$log = $login->check_login($username,$password);
if ($log) {
$login->redirect('home.php');
//$user_session = 'welcome'.' '.$_SESSION['username'];
} else {
// Registration Failed
$password_error='Wrong username or password';
}
}
$return_data=array('username'=> $username_error, 'password'=> $password_error ,'user_session'=>$_SESSION['username']);
header('Content-Type: application/json');
echo json_encode($return_data);
exit();
?>
login in class
public function redirect($url)
{
header("Location: $url");
}
You have some sort of output, which is causing "Headers already sent" error. You also have php errors disabled. Enable the errors and check it out (or check your apache error log).

How to check is someone is online?

So i have made a simple log in system in php how would i go on making a function on php to check if someone is online also can it be done in php
login system
<?php
if (empty($_POST) === false) {
$username = $_POST['username'];
$password = $_POST['password'];
if (empty($username) === true || empty($password) === true) {
$errors[] = 'Please Enter a username and password';
} else if (user_exists($username) === false) {
$errors[] ='Sorry but the user you entered dose not exist';
} else if (user_active($username) === false) {
$errors[] ='You have not activated your account please check your email to activate your account';
} else {
if (strlen($password) > 32){
$errors[] ='Passwords to long !';
}
$login = login($username, $password);
if ($login === false) {
$errors[] ='incorrect username or password';
} else {
$_SESSION['user_id'] = $login;
header('Location: index.php');
exit();
}
}
} else {
$errors[] = 'No information received';
}
echo output_errors($errors);
?>
You can use sessions for this:
http://php.net/manual/en/book.session.php
Simple usage of sessions:
// Start the session
if(!session_id())
session_start();
$_SESSION["the_user"] = true;
// To check if the session is alive
if(!empty($_SESSION["the_user"]))
echo "User is online !";
else
echo "User isn't online!";
// Delete the session
unset($_SESSION["the_user"]);
Note that this is just a simple usage of the session, the session will be alive even if the user went of the site. but it will be for a few minutes. (session's expire time)

handling errors on my login page

my error handling does not seem to work when i click the submit button without entering any of the fields,i can still login what exactly am i missing ?? its seems my validation is not working,
this is my code below
login.php
<?php
include 'core/init.php';
if (empty($_post) === false) {
$username = $_post['username'];
$password = $_post['password'];
if (empty($username) === true || empty($password) === true) {
$errors[] = 'You need to enter a username and password';
} else if (user_exists($username) === false ) {
$errors[] = 'Username not found';
} else if (user_active($username) === false ){
$errors[] = 'You haven\' activated your account';
} else {
if (strlen($password) > 32){
$errors[] = 'Password too long';
}
$login = login($username, $password);
if($login === false){
$errors[] = 'That username/password combination is incorrect';
} else {
$_session['user_id'] = $login;
header('location: index.php');
exit();
}
}
} else {
$errors[] = 'No data recieved';
}
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';
?>
my init.php
<?php
session_start();
require 'database/connect.php';
require 'functions/general.php';
require 'functions/users.php';
$errors = array();
?>
general.php
<?php
function sanitize($data){
return mysql_real_escape_string($data);
}
function output_errors($errors) {
return '<ul><li>' . implode('</li><li>', $errors) . '</li></ul>';
}
?>
Use $_POST instead of $_post, they both are different variable, as php is case-sensitive for variable names.

redirect based on user role stored in mysql database

I've made a website that users can now successfully login to but depending on which group the user is in, I would like to redirect them to different pages after logging in. I have a database with a row "training_group" and if for example, they are in group 2013_1, they would be directed to homepage_20131.php after logging in.
I've been looking for tutorials online and have found a possible solution with a switch function? but I am unsure of how/where to implement this. I just started learning php and would be grateful for any advice given!
Right now, my login page looks like this:
<?php
include 'core/init.php';
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';
?>
Here are a couple snippets that might get you going in the right direction.
function login($username, $password){
//... your login code .. database call
if($validLogin){
$user_id = id from database;
$group_id = id from database;
$return = array('user_id' => $user_id, 'group_id' => $group_id);
}
else{
$return = false;
}
return $return;
}
$userinfo = login($username, $password);
if ($userinfo === false) {
$errors [] = 'That username/password combination is incorrect';
}
else {
$_SESSION['user_id'] = $userinfo['user_id'];
$_SESSION['group_id'] = $userinfo['group_id'];
$page = 'homepage_' . str_replace('_', '', $userinfo['group_id'] . '.php';
header('Location:' . $page);
exit ();
}

We had problems activating your account

When email activate this error message "We had problems activating your account"
any problems my activate code..???
code give below..
'activate.php'
<?php
include 'core/init.php';
logged_in_redirect();
include 'includes/overall/header.php';
if (isset($_GET['success']) === true && empty($_GET['success']) === true) {
?>
<h2>Thanks, we've activated your account....</h2>
<p>You're free to Log in!</p>
<?php
} else if (isset($_GET['email'], $_GET['email_code']) === true) {
$email = trim($_GET['email']);
$email_code = trim($_GET['email_code']);
if (email_exists($email) === false) {
$errors[] = 'Oops, something went wrong and we could\'t find that email address';
} else if (activate($email, $email_code) === false) {
$errors[] = 'We had problems activating your account';
}
if (empty($errors) === false) {
?>
<h2>Ooops...</h2>
<?php
echo output_errors($errors);
} else {
header('Location: activate.php?success');
exit();
}
} else {
header('Location: index.php');
exit();
}
include 'includes/overall/footer.php';
?>
Email link code given below:
*'user.php'*
The activate function returns false. Make sure you call it in the correct way and that it works as expected.

Categories