I have simple application in codeigniter. I have login and logout in it. When I logged in.
member page is opened as in below screenshot. But when I click on back browser as arrow indicate, then it goes to login page. similar is the case when I logged out, login page is opened, but when I clicked on back browser as arrow indicate in below screenshot, then it goes to member page.
I want to validate it. I means it should be only in login page or member page, when I don't logged out or I don't logged in. when I press back browser arrow.
Session is used for this, or something else, but I don't know should I do it. Proper code for it is needed. Please help me friends. Thanks in advance.
You should make session control in login controller. You can use the code below:
if (!$this->session->userdata())
{
//put your login controller content here
}
else
{
redirect('your_dashboard_url');
}
You should use session in login controller
See below code:
public function __construct()
{
parent::__construct();
if (!$this->session->userdata('logged_in')){
redirect('login');
}
}
I hope it will work for you.
you can use session in codeigniter.check below link
https://www.codeigniter.com/user_guide/libraries/sessions.html?highlight=session
I try the following code,but still page is go back.
public function members()
{
if($this->session->userdata('is_logged_in'))
{
$this->load->view('admin/members');
}
else
{
redirect('main/login');
}
}
main is the controller's name and login and members are functions.
Related
I am writing a login file and I want to go back to the previous page and not the homepage after I successfully login.
public function LoginUser(){
if($checkEmail == 1){
header('refresh: 0 ; /')
}
}
Try this code. After successful login redirect to the desired page.
public function LoginUser(){
if($checkEmail == 1){
redirect('/provide your controller name/');
}
}
I recommended you store the previous page in a $_SESSION or temporary variable where you save your previous page because not all navigators can handle history.go(-1) and like this you can guarantee the user will redirect to the right page.
i have a secure login form and at the moment, i have set it to just redirect to a home page link, but i want to add some validation so that IF a user comes from a perticular page then they should be redirected to that page after logging in, but not sure how to do it, my current way is not working, here is what i have tried so far:
print $_SERVER['HTTP_REFERER'];
$previousPage = $_SERVER['HTTP_REFERER'];
if ($errors == "") {
if (do_login($form_email_address,$form_password)) {
// success!
if ($previousPage == "http://hiddensite/path/video/"){
redirect($previousPage);
}else{
redirect("/index.php?page=home&loggedin=1");
}
} else {
$errors = "Could not login. Please check your e-mail address and/or password and try again.";
}
and if your wondering what redirect() is, its just my function:
function redirect($url) {
// this function redirects from one page to another
ob_clean();
header("Location: $url");
exit();
}
Do you want to know if a user comes from another page on your own site? If so, you could add a session var to that previous page and test it on your login page.
On your previous page:
session_start();
$_SESSION['foo'] = "bar";
And on your login page -
if(isset($_SESSION['foo'])) {...}
Not a great way to keep track of referrers, but if you only want to check one page as per your question, this should work.
We seem to be missing some information in answering your question. The code you have provided seems fine but in order for us to pinpoint the issue we would have to see the do_login function as you previously posted in the comments (which is now retracted). Since you are always redirected to the home page that would mean that the do_login function always returns false or anything else but true.
Before you retracted the comment I did noticed you also used the sqlslashes() function a few times. Is this a function that you have created? Be sure to include this in your question.
I have a main page that has a log in link on it that takes the user to a login page. However, if a user is already logged in I want to take the user to another page, for example a info page.
I have this function:
function logged_in_redirect() {
if (logged_in() === true) {
header('Location: client.php');
exit();
}
}
I'm wondering where to put this? I've tried pretty much everything. If i put this on the login page, it does not redirect me when i am logged on. I've tried adding it as an onclick function for the link on the home page but it didn't work.
This is my logged_in() function:
function logged_in() {
return (isset($_SESSION['user_id'])) ? true : false;
}
Any suggestions?
Edit:
I have currently fixed the problem by making the button on the home page link to a test.php file which has this code:
<?php
include 'core/init.php';
if (isset($_SESSION["user_id"])) {
header('Location: client.php');
}
else {
header('Location: info.php');
}
?>
Is there any way around this?
If your session is set and the user is properly authenticated this will work.
You don't need extra function to check whether login is set unless you have a common file which is handling authentication related stuff and all the other files calling its function to check if the user is logged in..
Login Page:
<?php
//check if the user is already loggedin
if(isset($_SESSION["user_id"]){
//assuming client.php is in the same directoy
header("Location: client.php"); //you don't need exit since it will be redirected
}
//your login stuff. if your user_id was not set this part will be executed
?>
Also don't forget to destroy session once you log out..
I am using the Codeigniter redirect() to redirect the page after user logouts on my website http://www.theindianclassified.com.
When user goes to the url http://www.theindianclassified.com/logout he is redirected to the home page after he is logged out of the system. But if the user login and again click logout user is not logged out but he is redirected to the home page. I think the redirect using the Codeigniter redirect() method is cached by browser.
Please help me, how the implementation happen in the above scenario. I want the browser not to cache the redirect.
Log out function is below.
function logout() {
$this->session->sess_destroy();
redirect('');
}
You should check your login function to make sure the fault isn't there. If I were to guess it'll be there. If that doesn't work try 'unsetting' the session variables one-by-one.
I'm currently learning the basics of MVC using codeigniter as my framework and I created a login page. Now I already redirected a user after he logged in and stored a session data but the problem is after I log in (where in I redirected into another page) then pressed the back button the login page still displays unless I highlight the URL in the address bar and pressed enter wherein I go back again to the redirected page. Well I'm aware that this is because of the session check I've made but I need a solution on how I can solve it for the user to not go back again to login page unless he logged out. Same thing for logged out user not to go back to a certain page unless he/she logs in again.
To give a more details what exactly I wanted to happen is just like in facebook login wherein after you logged in and clicked the back button on browser the facebook will still redirect you to your homepage and not on the login page. samething goes to logout.
Well here's my controller so far I think this explains it all. Please do correct me if I made some redundancies in my code and understand that I'm a newbie on this. :)
public function index()
{
$data['error'] = null;
$username = $this->input->post('username');
$password = $this->input->post('password');
$data['login'] = $this->login_model->get_login_credentials($username,$password);
$data['session'] = $this->login_model->get_session($username);
//CHECK IF THE $PAGE VIEW IS EXISTING
if ( ! file_exists('application/views/pages/login.php'))
{
// IF PAGE DOESN'T EXISTS SHOW 404 ERROR
show_404();
}
if(($this->input->post('login')=='Login'&&$data['login']==1)||($username==$data['session']['username'])){
//SUCCESS LOGIN
$this->load->helper('url');
redirect('news');
}
if($this->input->post('login')=='Login'&&$data['login']!=1){
//LOGIN FAILED
$data['error'] = "Invalid login credentials.";
}
//FOR THE TITLE PART
$data['title'] = ucfirst('login');
$this->load->view('pages/login/login', $data);
$this->load->view('templates/footer', $data);
}
First for checking session data you can use :
$this->session->userdata('item');
here
$username==$data['session']['username'])
And perhaps you can SET 1 check at the start of your index as :
if($this->session->userdata('username'))
redirect('news');
Dont forget to load all helpers and libs required before callign those functions..
For sessions check http://codeigniter.com/user_guide/libraries/sessions.html
If you don't want to to go to login page after successful login, you can test the session in login page and redirect into some other page if session is set. Try this code.
if ($data['session']['username']) != NULL) {
redirect('home_page','refresh');
}
This question is already solved here: Clear cache on back press to prevent going back on login page or previous page after logout by Hashem Qolami Same question but in different perspective.