This forum is archived. Posts are preserved for historical reference. For current help, join us on Discord.

Redirect After Login based on Username

In UserSpice 4.4 · Started by maxikz on 2019-04-23 7:39 pm · 28869 views · 12 replies

I need to redirect after login based on user name ?
Let's say if user John log in he would be redirected to /john/index.php if 
User Michael log in he would be redirected to /micahel/index.php
Sure no problem. In users/scripts there is a file called custom_login_script.php

In there just put Redirect::to($us_url_root.$user->data()->username."index.php");

I'm on my phone so if that is not it it is pretty close
thank you, so it does work only for 1 user. here what I did..

Redirect::to($us_url_root.$user->data()->admin."101/index.php");
Redirect::to($us_url_root.$user->data()->Steven."calendar/");

so if I log in with admin it redirect to correct page, if log in with Steve it take to admin page not to calendar/
can someone help me with this issue/project
Ok. So let me make sure I understand. So let's say your project is at https://example.com.

If you want to direct the user named steven to a folder that is at https://example.com/steven

Then you would probably want to first make sure that everything is lowercase, so maybe you would do...
$url = strtolower($user->data()->username);

Note that if you go into the database, then username is the COLUMN in the database in the users table. That's where the information is coming from.

So to redirect to https://example.com/steven if steven is the username then you would do
Redirect::to($us_url_root.$url);

This is because $us_url_root already has the / at the end.
if you want to do something beyond that, like calendar you can definitely just add something to the end of the url.

So one more thing. This requires you to create a custom folder for every single user. Normally with php, people will have one calendar.php file and just load the information of the logged in user. Is there any particular reason of why you want to do it this way?
so you previous code worked kind of.
this is what I have

1. Redirect::to($us_url_root.$user->data()->id->admin. "101/index.php")   ;
2. Redirect::to($us_url_root.$user->data()->id->Steven."calendar/");

BUT
no matter what user I use to log in,  ether admin or Steven it takes me to 101/index.php , it doesn't recognize username and redirects to proper page.
Basically I want to redirect multiple use to their own homepages.
Can you explain what admin and Steven are?
are they pages or folders? How does the id fit into it? Are you using the id in the foldername?
admin and Steve are usernames.
Ok. So....
1. Redirect::to($us_url_root.$user->data()->id->admin. "101/index.php") ;
will redirect you to something that doesn't exist because $user->data()->id->admin doesn't exist. I also don't know what the 101 is

If you are trying to get a user named admin to go to a link like mydomain.com/admin101/index.php that is
Redirect::to($us_url_root.$user->data()->username. "101/index.php") ;

If you just want him to go to mydomain.com/admin/index.php that is
Redirect::to($us_url_root.$user->data()->username. "/index.php") ;


2. Redirect::to($us_url_root.$user->data()->id->Steven."calendar/");
if you are trying to redirect a user named steven to mydomain.com/Steven/calendar that is
Redirect::to($us_url_root.$user->data()->username. "/calendar") ;

$user->data()->username will output the username of the logged in user, so you don't need to type it out.
ok so username. = user that just logged in..

what I am trying to accomplish ::> 
  if user= admin log in send to to x.html
 if user = Steven send him to w.html 
if user = Alex send him to 564.html ?
Ok, so in usersc/includes/custom_login_script.php

if($user->data()->username == 'admin'){
Redirect::to('x.html');
}elseif($user->data()->username == 'Steven'){
Redirect::to('w.html');
}elseif($user->data()->username == Alex){
Redirect::to('564.html');
}
great this is exactly what I needed.
Thank you very much!

ok so username. = user that just logged in..

what I am trying to accomplish ::> 
  if user= admin log in send to to x.html
 if user = Steven send him to w.html 
if user = Alex send him to 564.html ?

$url = strtolower($user->data()->username);
Redirect::to($us_url_root.$url);

John goes to example.com/john
James goes to example.com/james

Or if you want to redirect to other pages, add a field in the users database table that contains the destination
// this adds a field into the users table
// this field holds the page to redirect to

ALTER TABLE users ADD redirect_page VARCHAR(100); // create a new field in the users table to hold the redirect_page

//then add the actual page to redirect to. do this for every user that needs redirected

$d = DB::getInstance();

$query = $db->query("UPDATE users 
SET redirect_page = 'https://example.com/x.html' 
WHERE username = 'john' ");

$results = $query->results();

Now when john logs in, you can..

$query = $db->query("SELECT redirect_page from users WHERE username = 'john' ")'
$results = $query->results();

Now you have the page to redirect to inside $results. now you can do the redirect.

My code is not exact. just an example and an idea for you.