In messaging system of my project when you get a message from a user you a email alert saying that the another user has sent a message to view the message click here (i.e the url of message) So if the user is not logged in to system he gets redirect to login page and after login it should get back to the referer url. I have made a basecontoller in core folder and extending the CI_controller the authenticating code is as follows.
function authenticate($type = 'user')
{
if($type == 'user')
{
if($this->user_id)
{
// user is logged in. check for permissions now
}
else
{
// user isnt logged in. store the referrer URL in a var.
if(isset($_SERVER['HTTP_REFERER']))
{
$redirect_to = str_replace(base_url(),'',$_SERVER['HTTP_REFERER']);
}
else
{
$redirect_to = $this->uri->uri_string();
}
redirect('user/login?redirect='.$redirect_to);
exit;
}
}
if($type == 'admin')
{
if($this->session->userdata('admin_id') && $this->session->userdata('user_type') ==5)
{
// Admin is logged in
}
else
{
redirect('admin/login');
exit;
}
}
}
The referer url is "http://example.com/project/pm/view_conversation?id=11"
now the problem is I am getting referer url till view_conversation and not able to get the id part.
Any suggestion ?
Thank you.
This can help:
CI 2+
https://www.codeigniter.com/userguide2/libraries/user_agent.html
CI 3+
http://www.codeigniter.com/userguide3/libraries/user_agent.html
Below solution is for Codeigniter version 3
$this->load->library('user_agent');
if ($this->agent->is_referral())
{
echo $this->agent->referrer();
}
UPDATE: interesting and useful information on how to obtain referrer information with the same user_agent library
https://www.tutorialandexample.com/user-agent-class/
How about just
redirect($_SERVER['HTTP_REFERER']);
Using php's $_SERVER global variable.
This worked for me!
Put that code in your Login Controler
function index() {
$this->load->library('user_agent'); // load user agent library
//Set session for the referrer url
$this->session->set_userdata('referrer_url', $this->agent->referrer() );
}
After Login Redirection Code
// user is authenticated if referrer is there
if( $this->session->userdata('referrer_url') ) {
//Store in a variable so that can unset the session
$redirect_back = $this->session->userdata('referrer_url');
$this->session->unset_userdata('referrer_url');
redirect( $redirect_back );
}
Because you have double question mark in the url, the browser ignores the url part after the second one. Use urlencode for you redirect part, like so:
redirect('user/login?redirect='.urlencode($redirect_to));
I've tested it out and it works this way.
By default CI is configured to ignore the query part of the URL (the part after the '?').
See: http://codeigniter.com/user_guide/general/urls.html
Related
Okay, so this is what I do:
I go to www.mywebsite.com/orders?id=1
It redirects be to login before proceeding.
I log in successfully but it redirects to www.mywebsite.com/orders.
If I am already logged in and go directly using GET method, it works fine. But if I am asked to login, the GET method disappears.
How do I preserve ?id=1?
Before redirecting the user back to the login page store the current page (the requested page) in a session variable. Assuming you have a function called check_login this would more or less look like what you should do:
public function check_login() {
if (!$this->session->has_userdata('logged_in') || $this->session->logged_in !== true) {
if (!empty($_SERVER['QUERY_STRING'])) {
$uri = uri_string() . '?' . $_SERVER['QUERY_STRING'];
} else {
$uri = uri_string();
}
$this->session->set_userdata('redirect', $uri);
redirect('/auth/login');
}
}
Then when the user successfully logs in your login function should somewhere have the following logic:
public function login() {
// form validation
// get post vars
// check username/pwd against db
if ($login) {
if ($this->session->has_userdata('redirect')) {
redirect($this->session->redirect);
} else {
redirect('/dashboard');
}
} else {
// error logging in
}
}
session variable could store the id.While log in using session pass the id value.You can retrive the value anywhere in session.
$this->load->library('session');
$this->session->set_userdata('userId', 'YourId');
where userId would be the name of the session variable, and YourId would be the value.
Simply Use this
redirect($_SERVER['HTTP_REFERER']);
I have a page that is restricted to logged in users (say it's called '/authed-page'). When a non-authenticated user visits this page they are prompted to click a button to login and they are then taken to the login url: '/login'
Now after they login I want them to get redirected to the page they had been trying to visit, '/authed-url', but I can't figure out how to do this!
Things I've tried:
1) Declaring a function redirect_to_authed_url in my functions.php file like this
function redirect_to_authed_url( $redirect_to, $request, $user ) {
return "/post-a-job";
}
Then in the template for authed-url.php when I detect the user isn't logged in I do this:
<?php add_filter( 'login_redirect', 'redirect_to_authed_url', 10, 3 ); ?>
However this doesn't work I think because the user presses a button which generates another http request which sends them to the '/login' url which means the redirect filter is lost.
2) Setting a cookie when someone visits '/authed-url' and they are not logged in called 'redirect_url'. Then I declare this in my theme's functions.php file
function redirect_request( $redirect_to, $request, $user ) {
if (isset($_COOKIE['login_redirect']) && strpos($_COOKIE['login_redirect'], 'http') === false) {
$redirect_url = $_COOKIE['login_redirect'];
unset $_COOKIE['login_redirect'];
return $redirect_url;
} else {
return('/');
}
}
This doesn't seem to work because I can't figure out where to set the cookie. I keep getting the warning:
WARNING: CANNOT MODIFY HEADER INFORMATION - HEADERS ALREADY SENT
I feel like I must be missing something obvious. This should be a pretty common requirement no? Any ideas?
You are able to append a URL encoded redirect_to variable to the login URL to get it to redirect after login.
For example:
http://www.example.com/wp-login.php?redirect_to=http%3A%2F%2Fwww.example.com%2Fauthed-url
That URL will redirect you to /authed-url after login.
you can try this function to check user logging or not.
<?php
if( is_user_logged_in() ) {
echo 'your function for logging user';
}else{
echo 'your function for not logging user';
}
?>
In my wordpress project i created a front end log in for users and when user enters a wrong log in information it must redirect the login failed attachment with the url. Below code does it for the first time and then it append to the url continuously when trying to log in. How can i limit this.
add_action('wp_login_failed', 'my_front_end_login_fail');
function my_front_end_login_fail($username){
// Get the reffering page, where did the post submission come from?
$referrer = $_SERVER['HTTP_REFERER'];
// if there's a valid referrer, and it's not the default log-in screen
if(!empty($referrer) && !strstr($referrer,'wp-login') && !strstr($referrer,'wp-admin')){
// let's append some information (login=failed) to the URL for the theme to use
wp_redirect($referrer . '&login=failed');
exit;
}
}
Any helps would be appreciated.
try this code
add_action('wp_login_failed', 'my_front_end_login_fail');
function my_front_end_login_fail($username){
// Get the reffering page, where did the post submission come from?
$referrer = $_SERVER['HTTP_REFERER'];
// if there's a valid referrer, and it's not the default log-in screen
if(!empty($referrer) && !strstr($referrer,'wp-login') && !strstr($referrer,'wp-admin')){
// let's append some information (login=failed) to the URL for the theme to use
////already not login failed append
if(!strstr($referrer,'&login=failed'))
{
wp_redirect($referrer . '&login=failed');
}
else
{
////if alreday append go to same url
wp_redirect($referrer);
}
exit;
}
}
How would I check if the user did not come from the same website? So for example if a user types in the URL in the browser to a direct page then they would have accessed that page without coming from another page in the site.
So something like:
if(!user not from same url)
{
//do some stuff
}
$this->referer() in controller?
Edit: Well, for your purpose, you can directly use this:
$referer = env('HTTP_REFERER');
if(empty($referer)){ echo 'jazz'; }
because $this->referer() behaves a little differently. It won't return false or null or empty string.
$referer = env('HTTP_REFERER');
if(empty($referer) && $this->Session->check('Auth.redirect') == false)
{
echo 'user didn\'t come website and wasn\'t redirect from the auth component';
}
Ok, having trouble here:
I created a login script, so after a person logs in then they will get direted to another page. And also, I have it redirecting them to the login page if they try and access one of those other pages.
My problem is, if a user is logged in and stumbles to the login page again --by accident-- I would like for it to recognize that the user is logged in and redirect them to that next page (which is index2.php) ?? Having troubles :-(
Here is my code so far:
require_once "inc/functions.class.php";
$quickprotect = new functions('inc/ini.php');
if (isset($_SESSION['goAfterLogin'])){
$goto = $_SESSION['goAfterLogin'];
unset($_SESSION['goAfterLogin']);
}
else $goto = $quickprotect->settings['DEFAULT_LOGIN_SUCCESS_PAGE'];
if (isset($_POST[username])) {
if($quickprotect->login($_POST[username], $_POST[password])) header ("Location: $goto");
}
Here is how I store a users session in the functions page
public function is_logged_in() {
//Determines if a user is logged in or not. Returns true or false;
if ($_SESSION['logged_in'] === md5($this->settings[ADMIN_PW])) {
return true;
}
else return false;
}
You don't mention how you store your users in your session, but something like this should do it for you:
if(isset($_SESSION['user']))
{
header("Location: index2.php");
exit;
}
This will check if you have a user in your session, and if so, redirect to index2.php.
You need to change 'user' according to your session key.