I am developing a web application with codeigniter. I have a main screen which loads three views as follows:
1)header 2)main screen 3)footer
the 'main_screen' view loads another view called 'login'
here is the code
<?php
if($logged == null){
$CI = &get_instance();
$CI->load->view('login');
}else{
echo $logged;
$CI = &get_instance();
$CI->load->view('login');
}
?>
the variable $logged is actually a data sent by a controller, when I first time loads the page it gives error 'Undefined, variable', but when i login it doesn't, here is the code.
if($loginSucessfull){
$data['logged'] ='<div class="success">Login Sucessfull</div>';
$this->load->view('header');
//here I am sending data to the 'menu_screen'
$this->load->view('menu_screen',$data);
$this->load->view('footer');
} else {
$data['logged'] = '<div class="alert">Sorry the username or password is incorrect</div>';
$this->load->view('header');
//here again I am sending the data
$this->load->view('menu_screen',$data);
$this->load->view('footer');
}
What I am trying to do is, if the user is sucessfully logged in show the sucessfull message else show the error and the login form as well. please help me how to fix this.
$logged isn't null. You get the error because the variable does not exist.
You have to set $logged to something before the main_screen page loads.
I don't know whether it works or not. Just try:
data = array();
$data['logged'] = 'something that you want to assign';
Related
Got this question here (sorry for being stupid), Just started with Codeigniter recently.
I have a login-system working fine. I tried to go to homepage while logged in with code on index-header.php:
<?php
if( !isset($_SESSION) ){
echo 'Login';
} else {
echo 'Log Out';
}
?>
And on main_view.php (homepage controller)
public function __construct() {
parent::__construct();
$this->load->library('session');
}
public function index() {
if($this->session->userdata('is_logged_in')){
$data['title'] = "Home";
$this->load->view('headfoot/header-main',$data);
$this->load->view('main_view');
$this->load->view('headfoot/footer-main');
} else {
$data['title'] = "Home";
$this->load->view('headfoot/header-main',$data);
$this->load->view('main_view');
$this->load->view('headfoot/footer-main');
}
}
}
Now, if I click logout from homepage while still logged in, it disconnects the session fine but doesn't change text back to "Login" in homepage after refresh.
In other words, it always shows text as "Logout" whether or not user is logged in.
dashboard.php (controller)
public function logout() {
$this->session->sess_destroy();
$data['title'] = "Logged out";
$data['logout_msg'] = "You have successfully logged out.";
$this->load->view('headfoot/header-login',$data);
$this->load->view('admin/login', $data);
$this->load->view('headfoot/footer-login');
}
Is it a good practice to create is_logged_in.php a separate file? If yes then how to link sessions to it?
change this:
<?php
if( !isset($_SESSION) ){
to:
<?php if($this->session->userdata('username') == null ){
i'm using 'username' here by assuming you have set username as session data when you allow user to log in.
Use $this->session->sess_destroy(); to kill session. Check this URL. Kill session by controller then use redirect.
By the way, create a file under application/core folder called MY_Controller.php and make your session things on it. If you want to learn more just google it.
I am using Joomla 2.5 and in the head of my site I have a module with the following code:
<?php
$user =JFactory::getUser()->guest;
if ($user->guest) {
echo 'Please sign up or sign in to view this'
}
else {
echo 'Here is the content';
}
?>
What I tried but didnt work is to add a variable $member in the first if statement. If the person isnt logged in to show him the echo but also set $member to == true.
Why I want to do this? I think it would make the site load faster if I check the status of $member than to go calling the Joomla API to determine if the user is logged in or not.
The calls to determine if a visitor is a member or not in every page are about 5-7. When I use the JFactory::getUser() function (all of the code above) 5-7 times it does slow down a bit the website. Thats why I thought of creating in the first call the $member == true correct me if I am wrong in thinking this would be better in site perfomance/speed wise.
I tried this:
<?php
$user =JFactory::getUser()->guest;
if ($user->guest) {
echo 'Please sign up or sign in to view this'
$member == true;
}
else {
echo 'Here is the content';
}
?>
I have the above code in the header module. But when I later in other modules in the same page or in the article if I use a php code it doesn't recognize the status of $member and always determines the user as not logged in.
What am I doing wrong? I'm guessing its something with variables scope, I checked my PHP book but couldn't find the error. Any help appreciated.
You are already calling $user->guest, so try changing:
$user = JFactory::getUser()->guest;
to this:
$user = JFactory::getUser();
As an alternative, you could also do use the following:
$user =& JFactory::getUser();
if($user->id=0){
echo 'Please sign up or sign in to view this'
$member == true;
}
else {
echo 'Here is the content';
}
You could do this in one line, Refactored code would be
$member = (JFactory::getUser()->id) ? true : false ;
I am using codeigniter on my project, and I have a login module and I authenticate each module. When the session does not exist then redirect to the login page. So, this works but sometimes it may happen when I login and then refresh the page I get redirected to the login page (meaning the session is gone).
Please do not try to tell me to use another library such as php native etc. I just want to know what the problem causing this is.
function _admin_login()
{
$this->form_validation->set_rules($this->login_rules);
$data['title'] = "Login";
$data['form_url'] = $this->uri->uri_string();
$data['login_btn'] = form_submit('login','Sign In','class="btn btn-large btn-block btn-primary"');
if($this->form_validation->run($this) == FALSE){
$data['user'] = form_input('username','','class="input-block-level input-large" placeholder="Username"');
$data['passw'] = form_password('password','','class="input-block-level input-large" placeholder="Password"');
$this->template->set('title','Login');
$this->template->load('template_login','login_view',$data);
}else{
$username = $this->input->post('username',true);
$row = $this->authentication_model->get_user_info($username);
$this->session->set_userdata('user',$row['user_id']);
$this->session->set_userdata('username',$row['username']);
$this->session->set_userdata('login_state',true);
$this->authentication_model->update_last_login();
redirect('product');
}
}
This is my login script, if the validation passes then the session is set... and in every controller I have checked the session script. Example below:
$this->authentication_plugin->check_logged_in();
function check_logged_in()
{
if(logged_in() === FALSE){
set_alert('Login in to view this page!!','error');
set_bookmark('login_url');
redirect("login");
}
}
function logged_in()
{
$CI =& get_instance();
$user = $CI->session->userdata('user');
if(!$CI->load->library('session')){
echo "no session is loaded";
die;
}
if(!empty($user)){
return true;
}else{
return false;
}
}
Any ideas? Thanks!
autoload the session library and remove that from your login script.
In config/autoload.php look for $autoload['libraries']
add 'session'
in config/config.php
scroll down to cookie configs and look for $config['sess_cookie_name']
make sure there is no underscore in the cookie name. you have to change the codeigniter default name to do this.
test by doing session checks in the constructor. its a quick sanity check to see if the sessions are working or not.
I have an installation of Codeigniter, IonAuth + Hybridauth which I am reworking so my users can choose their own username instead of generating one using the first and last names returned by facebook.
So in my code below I check to see if a username was posted, if not I want to load the choose_username view but for some reason the view is not loading and its completely skipping that section which is why I added die('Why no view')
Update: This first piece of code runs fine in a new controller
Checkout the code here:
if(isset($_POST['username'])){
$username = $this->input->post('username', TRUE);
die($username);
}else{
$this->data['message'] = 'Please choose a username.';
$this->data['template'] = 'guests/partials/choose_username';
$this->load->view('guests/template/standard', $this->data);
die('Why no view?');
};
Longer version:
function login_provider($provider = '')
{
if(empty($provider)) redirect();
try
{
// create an instance for Hybridauth with the configuration file
$this->load->library('HybridAuthLib');
if ($this->hybridauthlib->serviceEnabled($provider))
{
// try to authenticate the selected $provider
$service = $this->hybridauthlib->authenticate($provider);
if ($service->isUserConnected())
{
// grab the user profile
$user_profile = $service->getUserProfile();
////////////
//var_dump($user_profile);
//die();
////////////
$provider_uid = $user_profile->identifier;
if($this->ion_auth->login_by_provider($provider,$provider_uid))
{
$data['user_profile'] = $this->ion_auth->user_by_provider();
//$this->load->view('auth/user_profile',$data);
$user = $this->ion_auth->user()->row();
//Redirect to custom subdomain
$url = explode('://',site_url());
if (strpos(site_url(),$user->username) !== false) {
redirect($url[0].'://'.str_replace('www','',$url[1]).'dashboard','refresh');
}
else{
redirect($url[0].'://'.$user->username.str_replace('www','',$url[1]).'dashboard');
};
}
else
{ // if authentication does not exist and email is not in use, then we create a new user
//Check if username was posted
if(isset($_POST['username'])){
$username = $this->input->post('username', TRUE);
die($username);
}else{
$this->data['message'] = 'Please choose a username.';
$this->data['template'] = 'guests/partials/choose_username';
$this->load->view('guests/template/standard', $this->data);
die('Why no view?');
};
So when I run the above code, all i get is a blank page with: Why no view.
As above, usually when I run into this sort of issue it's from a bug in the view code.
Also, I don't know what, is actually being passed by this post in the event of there not being username data but you might want to also be checking for an empty value for username. This is probably not the issue but it would be good to confirm that the initial if is evaluating the way you expect.
I have the following code. Checks if the user is logged in or not.
When the variable $is_logged_in is not set or is False, I load a message view.
Unfortunately, at the same time the system loads the restricted content view.
So I used die() function, and now only shows a blank page.
What can I do to only load the message view when the user is not logged in?
Thanks.
if(!isset($is_logged_in) OR $is_logged_in == FALSE)
{
$data['main_content'] = 'not_logged_in';
$data['data'] = '';
$this->load->view('includes/template',$data);
die();
}
Actually, I've found an answer to mantain the URL and not redirect:
$data['main_content'] = 'unauthorized_access';
$this->load->view('includes/template', $data);
// Force the CI engine to render the content generated until now
$this->CI =& get_instance();
$this->CI->output->_display();
die();
Anyway. I used a redirect to the login page, and a flashdata variable
if(!isset($is_logged_in) OR $is_logged_in == FALSE)
{
$this->session->set_flashdata('error_msg','You must be logged in to access restricted area');
redirect('login/');
}
Thanks
I've been messing around with this for a while. If you're using die or exit after trying to load a view, CI shows a blank page.
The solution would be to use return, which stops execution of the current function, and does not execute anything after below that.
A simple example:
public function validate(){
//validation code
if(!$valid){
$this->load->view('error');
return;
}
//This code won't run
}
CI is probably using output buffering (see http://www.php.net/manual/en/ref.outcontrol.php). If you want to load a view and kill the script, you will need to flush the buffer. This would normally be done at the very end of the script, but die()ing stops it from getting there.
if(!isset($is_logged_in) OR $is_logged_in == FALSE)
{
$data['main_content'] = 'not_logged_in';
$data['data'] = '';
$this->load->view('includes/template',$data);
ob_flush();
die();
}