Closed. This question is not reproducible or was caused by typos. It is not currently accepting answers.
This question was caused by a typo or a problem that can no longer be reproduced. While similar questions may be on-topic here, this one was resolved in a way less likely to help future readers.
Closed 8 years ago.
Improve this question
im getting error for this code.it says syntax error, unexpected T_PUBLIC in C:\wamp\www\openarc\application\controllers\login.php on line 32. i think a curly bracket is missing. but cant find it where. pls help me
<?php
/**
* This class use to registered users login to the site and logout
*/
class Login extends CI_Controller{
/*load the login page*/
function index($pass_details=false)
{
//if user is already logged in restrict login page to user
if($this->isLoggedin())
{
redirect('/');
}
//if user is not logged in
else
{
//if login failed pass $pass_details message to login view
if(isset($pass_details))
{
$login_messeage['loginErrorMessage'] = $pass_details;
// $login_messeage['userName'] = $pass_details['user_name'];
}
$login_messeage['page_id'] = "login";
//pass error message and user_name to view and show login page
$this->load->view("template",$login_messeage);
}
}
}
/*take the login form values and do the back end validation and send those values to the Login_model*/
public function user_login()
{
if(!$this->isLoggedin())
{
$username = $this->input->post('lg_username');
$password = $this->input->post('lg_password');
$url = $this->input->post('hidden_url');
//load the login_model
$this->load->model('Login_model');
//create an array and pass username and password
$user_login_details =array(
'username' => $username,
'password' => sha1($password),
);
//pass $user_login_details array to the Login_model get_specific_record function
$result = $this->Login_model->get_specific_record($user_login_details);
//get executed result counter of num_rows into $numrows variable
$numrows = $result->num_rows();
//get database values to variable into variables from executed result
if($numrows>0)
{
foreach ($result->result() as $row)
{
$uname = $row->username;
$upassword = $row->password;
$userid = $row->login_id;
$usertype = $row->user_type;
$userrefid = $row->reference_id;
}
if($usertype == 'Ad')
{ //echo "ggg";exit();
//check executed result num_rows() counter is grater than 0
//user details set to sessionArray to set session
$sessionArray = array(
'username' =>$uname,
'userType' =>$usertype,
'refId' =>$userrefid,
'login_status' => true
);
//create session and redirect user to home page(index page)
$this->session->set_userdata($sessionArray);
redirect('adminpanel');
}
else if ($usertype == 'Op') {
//user details set to sessionArray to set session
$sessionArray = array(
'username' => $uname,
'userType' => $usertype,
'refId' => $userrefid,
'login_status' => true
);
//create session and redirect user to home page(index page)
$this->session->set_userdata($sessionArray);
redirect('Production');
}
else if($usertype == 'C')
{
//user details set to sessionArray to set session
$sessionArray = array(
'username' => $uname,
'userid' => $userid,
'userType' => $usertype,
'refId' => $userrefid,
'login_status' => true
);
//create session and redirect user to home page(index page)
$this->session->set_userdata($sessionArray);
$cartSessionArray = array(
'user_id' => $userid,
'status' => 'A'
);
$this->load->model('Cart_model');
$cart_result = $this->Cart_model->get_all_cart_data($cartSessionArray);
$cart_numrows = $cart_result->num_rows();
if($cart_numrows >0)
{
foreach ($cart_result->result() as $cart_row)
{
$cart_id = $cart_row->id;
$cart_name = $cart_row->name;
$cart_price = $cart_row->price;
$cart_qty = $cart_row->qty;
$insert_cart = array(
'id' => $cart_id,
'name' => $cart_name,
'price' => $cart_price,
'qty' => $cart_qty
);
$res = $this->cart->insert($insert_cart);
}
if($res)
{
if ($url == 'index')
{
redirect('place_order');
}
else
{
redirect($url);
}
;// redirect('/products');
}
else {
if ($url == 'index')
{
redirect('place_order');
}
else
{
redirect($url);
}
}
}
else
{
redirect($url);
}
}
else if($usertype == 'Ma')
{
//user details set to sessionArray to set session
$sessionArray = array(
'username' => $uname,
'userid' => $userid,
'userType' => $usertype,
'refId' => $userrefid,
'login_status' => true
);
//create session and redirect user to home page(index page)
$this->session->set_userdata($sessionArray);
redirect('management/monthly_order_count');
}
}
else
{
$pass_details = "<div class='alert alert-error'>Username or password is Wrong</div>";
$this->index($pass_details);
}
}
else
{
redirect('/');
}
}
/*logout the loged user*/
public function logout()
{
if($this->isLoggedin())
{
//unset session data for user logout
$this->session->sess_destroy();
//redirect to the home page
redirect('/');
}
}
}
}
?>
Typo:
}
}
} <---this bracket is closing your object
/*take the login form values and do the back end validation and send those values to the Login_model*/
public function user_login()
Since you've got an extra bracket, your class definition prematurely terminates, making public useless, since that's valid only inside a class definition.
You are closing the class definition on line 31...
...
28 $this->load->view("template",$login_messeage);
29 } // end if
30 } // end function index
31 } // misplaced end class definition
Get rid of the line 31.
Related
I am having a trouble on displaying my admin's full name, like for example admin full name is John Doe, it is not displaying. I am still learning codeigniter please give me advice thank you!
here is my controller
//Get username
$username = $this->input->post('username');
//Get and encrypt the password
$password = $this->input->post('password');
// Login user
$user_id = $this->role_model->login($username, $password);
if ($user_id) {
// Create session
$user_data = array(
'user_id' => $user_id,
'name' => $user_id->name,
'username' => $username,
'logged_in' => true
);
$this->session->set_userdata($user_data);
//Set message
$this->session->set_flashdata('user_loggedin','You are now logged in');
redirect('pages/index');
here is my View - where I would like to display my full name, as you can see 'name' is the data field I have to display but it is does not show anything, it gives an error that says name is not defined.
<li><a> Welcome, <?php echo $this->session->name ?> </a></li>
Model
public function login($username, $password){
//Validate
$this->db->where('username',$username);
$this->db->where('password',$password);
$result = $this->db->get('users');
if ($result->num_rows() == 1) {
return $result->row(0)->id;
}else{
return false;
}
}
Your method login() returns only id = digit (return $result->row(0)->id;), not object (in controller your get $user_id->name).
Do this, in the model:
if ($result->num_rows() == 1) {
return $result->row(0); // fix
}else{
return false;
}
In the controller:
$user = $this->role_model->login($username, $password);
$user_data = array(
'user_id' => $user->id, // fix
'name' => $user->name, // fix
'username' => $username,
'logged_in' => true
);
It shows undefined because it is indeed undefined.
In your view your're trying to echo the returning value of a function called userdata(), does that function actually exist? Or is $this->session->userdata an array? in which that case, you need to use [ index ] to acces a member within an array. i.e. $this->session->userdata['name'], but also, that member needs to exist first.
controller :
if($this->Login_model->login_valid($email,$password))
{
$sessiondata = array(
'username' => $email
);
$this->session->set_userdata('logged_in', $sessiondata);
redirect('narson');
}
else
{
$this->session->set_flashdata('error',"Invalid Username And password");
redirect('login');
}
model:
public function login_valid($username,$password)
{
$this->db->where('username',$username);
$this->db->where('password',$password);
$query = $this->db->get('admin');
if($query->num_rows() > 0)
{
return true;
}
else
{
return false;
}
}
I have an Admin controller which handles the user login and other admin functionality like managing a basic cms. In the constructor I check if the users session present which is set once they login. If not and they try to access a restricted page, they should be redirected. If I redirect to another page then when going to the login page I get redirected right away as obviously that check is done in the constructor, but as soon as I try to redirect to the login page I get an error:
This page isn’t working localhost redirected you too many times. Try
clearing your cookies. ERR_TOO_MANY_REDIRECTS
class Admin extends Controller {
public function __construct()
{
if(!isLoggedIn()) {
redirect('admin/login');
}
$this->AuthModel = $this->model('Auth');
}
isLoggedIn is a function:
function isLoggedIn() {
if(isset($_SESSION['user_id']) && isset($_SESSION['browser']) && $_SESSION['browser'] == $_SERVER['HTTP_USER_AGENT']) {
return true;
} else {
return false;
}
}
Login method:
public function login()
{
if($_SERVER['REQUEST_METHOD'] == 'POST') {
$email = filter_var($_POST['email'], FILTER_SANITIZE_EMAIL);
$password = trim($_POST['password']);
$data = [
'email' => $email,
'password' => $password,
'email_error' => '',
'pass_error' => '',
'no_user' => '',
'pass_wrong' => ''
];
if(!filter_var($_POST['email'], FILTER_VALIDATE_EMAIL)) {
$data['email_error'] = 'Invalid email address <br />';
}
if(empty(trim($_POST['password']))) {
$data['pass_error'] = 'Password required';
}
if(!empty(trim($_POST['email'])) && !$this->AuthModel->getUserByEmail($email)) {
$data['no_user'] = 'Email does not exist';
}
if(!empty($data['email_error']) || !empty($data['pass_error']) || !empty($data['no_user'])) {
$this->view('admin/login', $data);
} else {
$loggedInUser = $this->AuthModel->login($email, $password);
if($loggedInUser) {
$this->CreateUserSession($loggedInUser);
} else {
$data['pass_wrong'] = 'Incorrect login details';
$this->view('admin/login', $data);
}
}
} else {
$data = [
'email' => '',
'password' => '',
'email_error' => '',
'pass_error' => '',
'no_user' =>'',
'pass_wrong' => ''
];
$this->view('admin/login', $data);
}
}
You are using same controller for login and other functionalities. Every time it checks for authentication and it gets no. And it loops over again. try to implement your login functionalities in a separate Controller.
I'm creating a session at login but it's not saving the variables I store inside the session nor is it carrying them across to other pages:
Controller code:
function CheckDatabase($password) //This function is only run when password validation is correct.//
{
$username = $this->input->post('Username'); //Sets the username as a $username.//
$result = $this->User_model->Login($username, $password);
if($result)
{
$sess_array = array();
foreach($result as $row)
{
$sess_array = array( //Makes an array of the data to be stored in the session.//
'UserID' => $row->UserID,
'Username' => $row->Username
);
$this->session->set_userdata('logged_in', $sess_array); //Sets $sess_array as the session.//
}
return TRUE;
}
else //Ran if the username or password aren't matched in the CIUsers database. Returns error message.//
{
$this->form_validation->set_message('CheckDatabase', 'Invalid login details.');
return false;
}
}
Index function on the same controller (which should stop a user going back to the login screen but doesn't)
function index() //Default function that is run when this controller is called.//
{
if($this->session->userdata('logged_in'))
{
redirect('Home_controller');
}
else
{
$this->load->helper(array('form'));
$this->load->view('Login_view');
}
}
Code on the home view (which browser is directed to once logged in)
<?php echo $UserID .": ".$Username; ?>
I get this error for displaying the session data:
Message: Undefined variable: Username
Filename: views/Home_view.php
Line Number: 9
To retrieve the session variable, you have to do like this
$username = $this->session->userdata['logged_in']['Username'];
echo $username;
You have to retrieve it from session like this before using:
$user = $this->session->userdata('logged_in');
$Username = $user['Username'];
$UserID = $user['UserID'];
Try like this
$newdata = array(
'user_id' => $rows->id,
'user_name' => $rows->name,
'user_image' => $rows->user_image,
'user_email' => $rows->email,
'logged_in' => TRUE,
'user_type' => $rows->user_type
);
}
$this->session->set_userdata($newdata);
And check in your controller construct for user session
<?php
if (!defined('BASEPATH'))
exit('No direct script access allowed');
class User extends CI_Controller {
public function __construct() {
parent::__construct();
//Here we will check for the session value if it is true then the function will run else it will redirect to login page
if ($this->session->userdata('logged_in') == FALSE) {
redirect(site_url('your login page'));
} else {
// if there is session value
redirect(site_url('User/your function name');
}
}
}
?>
For complete explaination read this tutorial
http://w3code.in/2015/10/session-handling-in-codeigniter/
if($result)
{
$sess_array = array();
foreach($result as $row)
{
$sess_array = array( //Makes an array of the data to be stored in the session.//
'UserID' => $row[0]->UserID,
'Username' => $row[0]->Username
);
$this->session->set_userdata('logged_in', $sess_array); //Sets $sess_array as the session.//
}
return TRUE;
}
I have the following code to identify a session for language_id OR set a session if a $_GET varaiable is passed. The following function detectLanguage() is called in my component Controller.php file inside init();
private function detectLanguage()
{
if(isset($_GET['language_code']) && !empty($_GET['language_code'])){
$language = Language::model()->find(array(
'condition' => 'iso_code = :iso_code',
'params' => array(':iso_code' => $_GET['language_code']),
));
if($language != NULL){
Yii::app()->session['language_id'] = $language->id;
$this->redirect(Yii::app()->request->urlReferrer);
} else {
$this->redirect(Yii::app()->request->urlReferrer);
}
} else {
$currentLang = (isset(Yii::app()->session['language_id']))? Yii::app()->session['language_id'] : 1;
if(Language::model()->find(array('condition' => "id = :id AND country_id = :country_id",'params' => array(':id' => $currentLang, 'country_id' => $this->country_details['ID'])))){
return $currentLang;
} else {
Yii::app()->session['language_id'] = 1;
return 1;
}
}
}
When I go to the URL ...?language_code=ru, it successfully sets the session and redirects to the referrer URL, but when I then refresh the page, it resets the language_id session to 1.
I do not understand, as far as I can see, my logic in this function is fine.
EDIT: Also tried with cookies and there is no other code interacting with these SESSION variables which could be interferring.
I want to add form validation if the ip_address duplicated just
stay on current page or show me any kind of message.
this is the model code
public function add_vote($option_id)
{
$this->db->insert($this->votes_table, array(
'option_id' => $option_id,
'ip_address' => $this->input->ip_address(),
'timestamp' => time()
));
return ($this->db->affected_rows() == 1) ? TRUE : FALSE;
}
this is the controler
public function vote($poll_id, $option_id)
{
if ( ! $this->poll_lib->vote($poll_id, $option_id))
{
$data['base_styles'] = 'public_html/res/css/base.css';
$data['title'] = 'Sorry an error occured';
$data['error_message'] = $this->poll_lib->get_errors();
$this->load->view('header');
$this->load->view('www/posts/postclose', $data);
}
else
{
redirect('posts', 'refresh');
}
}
hear if i add new vote it's shows me database error as the column is duplicate so i don't want to show that, if it redirect me to any other page will be great if stay on current page still ok or any pop up message.
Do a check before inserting into database.
public function add_vote($option_id)
{
$query = $this->db->get_where($this->votes_table, array('ip_address' => $this->input->ip_address()));
if($query->num_rows() < 1)
{ $this->db->insert($this->votes_table, array(
'option_id' => $option_id,
'ip_address' => $this->input->ip_address(),
'timestamp' => time()
));
return "Successfully Inserted";
}else{
return "Duplicate";
}
}
In your controller handle the response and display accordingly