What is the best way to force users to the edit profile page.
Here is my code :
$user = JFactory::getUser();
if ($user->email === "fakemail#spam-disposable-domain.com") {
//Redirect user to the edit profile page forcing them to change their email.
//index.php?option=com_users&view=profile&layout=edit
}
I want to redirect users who's emails match that to force them to edit their profile the correct way to redirect them to the edit profile page is what I need to know.
Use the redirect() method of the application object:
$app = JFactory::getApplication();
$user = JFactory::getUser();
if ($user->email === "fakemail#spam-disposable-domain.com")
{
$app->enqueueMessage('Please change your email address!');
$app->redirect(
JRoute::_(
'index.php?option=com_users&view=profile&layout=edit'
)
);
}
Related
In the AuthController.php I have a function:
public function authenticated( \Illuminate\Http\Request $request, \App\User $user )
{
return redirect()->intended($this->redirectPath());
}
I have login popup on every page so if the user logins from a page: /demo-page and logins, they will be redirected back to the same page(/demo-page) after login.
This works absolutely as expected. No problem here.
My Question: How can I redirect user to specific pages based on their current route from which the login was invoked.
Logic something like this:
if referring route('indexpage'){
redirect to route('homepage');
}
else any other route other than index page{
return redirect()-> to previous url
}
So previously I was looking at my AuthController file. But I forgot that I had changed the LoginController for the redirect
public function __construct()
{
$this->redirectTo = url()->previous(); //Redirect the user to previous page after login
$this->middleware('guest')->except('logout');
}
What I need to do:
Suppose there are 3 routes. A, B and C
First the user lands on page A
If he logins there he have to be redirected to page B.
If the user is on page B and he logins there, he have to be redirected back to page B.
If the user is on page C and he logins there, he have to be redirected back to page C.
Note: the login is a common form on header of every page.
What's currently working?
With return redirect()->intended($this->redirectPath()); this,
the user from A is redirect to A,
B to B
and
C to C.
This should work:
public function authenticated(\Illuminate\Http\Request $request, \App\User $user)
{
if ($to = $request->input('redirect_route')) {
$to = URL::route($to);
}
$to = $to ?: $this->redirectPath();
return redirect()->intended(
$to
);
}
Read the API here.
What you can do on the page where you restrict access to autorised users, use this code to set a url paramater called accesscheck:
$restrictGoTo = "login.php";
if (!((isset($_SESSION['Username'])) &&
(isAuthorized("",$authorizedUsers, $_SESSION['Username'],
$_SESSION['UserGroup'])))) {
$qsChar = "?";
$referrer = $_SERVER['PHP_SELF'];
if (strpos($restrictGoTo, "?")) $qsChar = "&";
if (isset($_SERVER['QUERY_STRING']) && strlen($_SERVER['QUERY_STRING']) >
0)
$referrer .= "?" . $_SERVER['QUERY_STRING'];
$restrictGoTo = $restrictGoTo. $qsChar . "accesscheck=" .
urlencode($referrer);
header("Location: ". $restrictGoTo);
}
And then on your login page. First thing you need to do is check if the url parameter accesscheck is present. It it is, redirect to the page after login, otherwise, go to the page specified.
if (isset($_GET['accesscheck'])) {
$_SESSION['PrevUrl'] = $_GET['accesscheck'];
}
//run your login script and if the user is authenticated proceed to the
previous url or the success page:
$redirectLoginSuccess = "yourpage.php";
if (isset($_SESSION['PrevUrl']) && true) {
$redirectLoginSuccess = $_SESSION['PrevUrl'];
}
header("Location: " . $MM_redirectLoginSuccess );
}
This works even if you use parameters in your url's
i am working on laravel socialite to login using gmail.i can able to store user info and if gmail id and email already exist then it should redirect to dashboard.
public function googleCallback()
{
$user = Socialite::with('google')->user();
$email=$user->email;
$user_id=$user->id;
if(Auth::attempt(['email' =>$email,'password'=>$user_id]))
{
//return redirect('user/UserDashboard');
//return Redirect::to('user/UserDashboard');
echo "i am here";
}
}
in the above code if i use echo then it will print .suppose if redirect to dashboard it wont work.i don't know why redirect wont work for socialite.Can anyone tell where i am doing wrong ?
thank you
Update:
suppose if i login to my site using gmail or fb or github then data will store in my db.if that person login second time then it should redirect to user dashboard. the above code will check if auth::attempt exist or not.suppose if i print any think in inside if() block it works.suppose if i add redirect to dashboard then login through gmail and all not working
I don't know till what extent my answer would be correct but it should go somewhat like this:
public function googleCallback()
{
$user = Socialite::with('google')->user();
$email=$user->email;
$user_id=$user->id;
$user_db = \App\User::where('email', $user->email)->first();
if (count($user_db) == 1) {
echo "i am here";
} else {
// Your registration process
// With Login process i.e. auth()->attempt(YOURDATA);
}
}
This is the only way that I could have done this thing. If anything else I can help in, put it in the comment box.
use this to redirect
return redirect()->intended('user/UserDashboard');
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';
}
?>
I made one of my menu items only for Guests (not logged in users). ANd If user logs in, it disappears from menu. It works fine...
But I noticed that when I put the link to the Guest menu item in browser, when I'm lnot ogged in , it shows very disgusting jos-Error: You are not authorised to view this resource.
I want to find a way to track such errors and redirect to custom 404 page or some other page.
Please, assist me in this issue.
Thanks
The string You are not authorised to view this resource located in JERROR_ALERTNOAUTHOR language string and fire in includes/application.php function:
public function authorise($itemid)
{
$menus = $this->getMenu();
$user = JFactory::getUser();
if (!$menus->authorise($itemid))
{
if ($user->get('id') == 0)
{
// Redirect to login
$uri = JFactory::getURI();
$return = (string)$uri;
$this->setUserState('users.login.form.data', array( 'return' => $return ) );
$url = 'index.php?option=com_users&view=login';
$url = JRoute::_($url, false);
$this->redirect($url, JText::_('JGLOBAL_YOU_MUST_LOGIN_FIRST'));
}
else {
JError::raiseError(403, JText::_('JERROR_ALERTNOAUTHOR'));
}
}
}
You could alter this function and add redirection or change the text string with your desired message.
Redirect could be done with redirect() method.
An example of redirect syntax:
$app = JFactory::getApplication();
$app->redirect(JRoute::_(JURI::root().'index.php'));
Please make sure that you won't alter application.php without creating an override system plugin.
Hope this helps
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