I have a website, got it developed from someone, Need help in redirecting back after login in php.

After Login it redirects to Home Page. But i want user to go back where he came from.

logincontroller.php

public function checklogin(Request $request){
    $phone = DB::table('users')->where('contact','=',$request->input('code').$request->input('contact'))->get();
    if(count($phone)>0){
        $user_data = array(
            'email' => $phone[0]->email,
            'password' => $request->get('password')
        );

        if(Auth::attempt($user_data)){
            return redirect('/');
        }else{
            return back()->with('error','wrong_details');
        }
    }else{
        return back()->with('error','wrong_details');
    }
}

Recommended Answers

All 14 Replies

It seems to me you want to replace return redirect('/'); with return back(); but without seeing the full library, I don’t know if you have to add with('success') or something like that the way that it’s done if login fails.

Sorry for my brief post but I’m typing from my iPhone.

Hello As i am new to code or know less about code. Can you please tell me what to share with you. i recently came to know the coding is in guzzle 6.3
i tried return redirect back() but it redirects to the login/signup page after login. but i want it to go back on the page where the visitor was. should i share my website's domain here so you can visit my website and register or check whats going on.

I'm at my computer right now so I can provide a more detailed explanation. It seems that the back(); method being used in your code takes the user back to the last page they were on. In the case of the form submission, that would be the page with the form. Oh well. It was worth a shot.

Unfortunately to take them back to the page where the visitor was is not nearly as easy as changing just a line or two. The reason is because the user was on Page A. Then they clicked to go to Page B (the login form). Then they submitted it and were taken to Page C (Login success page, which currently redirects back to the homepage). HTTP provides a built-in way to return to the page that you were last on, but by the time you get to Page C, there's no way of knowing where you were two pages ago (Page A).

You would actually have to modify your code such that Page B, which is aware of Page A, forwards the URL for Page A to Page C. Have I lost you yet?

Unfortunately there's no way for even an experienced programmer to do this without access to all of your code for the project. Then, they would have to read over all the code, make heads or tails of generally what the programmer you hired did, and then they can make the adjustment.

It would probably only take a maximum of 5 minutes for the person who wrote the code in the first place to make the adjustment, because they're already familiar with everything they wrote. I think the best situation here, given you are very new to code, might be to just go back to them and ask them to make the change, and you can learn what they did.

Alternatively, you can submit all of the code for your project as a .ZIP file attachment here on DaniWeb, but it might be beyond the scope of this free community for someone to take the time to sift through allll of the code for your project enough to make heads or tails of what's going on enough to make what would otherwise be a very simple change.

Just to clarify, all that needs to happen is that you need to pass the URL of Page A through the form on Page B, so that Page C will be aware of it. Page C can then do the proper redirect. However, without access to the full framework being used, I wouldn't begin to know the names of the methods being used.

For example, back() is not a built-in PHP function. It's a PHP function that was invented by the author of the framework / code being used in this project.

php laravel guzzle 6.3 this is the framework i think
and thank you for your time. please let me know if you can help with this information

Unfortunately I don't personally know laravel but I've added the tags laravel and guzzle to this topic to give it some attention from people who are more familiar.

That being said, if you shoot over the code for the login form, I could take a look at it. Knowing that this is based on laravel does help.

Curly Braces removed from some spaces
RegisterController.php

<?php

namespace App\Http\Controllers;

use Illuminate\Http\Request;

class registerController extends Controller
//
public function index()

    return view('includes/login');

LoginController.php

    if(Auth::attempt($user_data)){
        return redirect('/');
    }else{
        return back()->with('error','wrong_details');
    }

}

public function checklogin(Request $request){
    $phone = DB::table('users')->where('contact','=',$request->input('code').$request->input('contact'))->get();
    if(count($phone)>0){
        $user_data = array(
            'email' => $phone[0]->email,
            'password' => $request->get('password')
        );

        if(Auth::attempt($user_data)){
            return redirect('/');
        }else{
            return back()->with('error','wrong_details');
        }
    }else{
        return back()->with('error','wrong_details');
    }
}

i have shared two file's some code as i don't know which file's code should i share. so please let me know which file's code should i share.
and thanks for giving me your valuable time.

So it looks like the RegisterControler just includes view('includes/login') so I would need the includes/login file, wherever that is.

Sorry, I don't know laravel.

Please Suggest me whome I should Ask.

If you send the entire project to someone with Laravel experience, they should be able to do it in less than 5 minutes. Perhaps it’s worth paying $20 for. My recommendation to you would to either go back to the person who wrote it in the first place and ask if they’ll make this simple update, or to hire a freelancer on Upwork.

Also as mentioned I could take a look at it if you post all the code for this project.

Seems to me if the normal techniques for backing up, which uses the browser's history, I think, isn't working, then I think you have to write some custom code.

One thing that you might be able to do is to programmatically look at the browser's history so that you can back up anywhere into that list of pages. Scan back through that list until you come to the page just before the login page and that should be the one you want to redirect to.

If this doesn't work, or is too difficult, then you might just keep your own browser history by saving the url of each page in a list in the local storage. Then you can scan through this list to find the page to go back to. Now, this list might get long, so create it as a queue of a certain length, maybe 5 should be sufficient, and then prune the queue when you try to add the 6th element.

Just thinking on one foot here but this are the solutions that come to mind, what I might try if I was in your shoes. Neither techique might not work but I know that if I were doing it, and neither way worked, I think I'd learn enough about my site and how things work to come up with other ways to try and get the result I want.

Have a good time experimenting and digging into your site and how browsers work. Doing this kind of stuff is what I call fun, you see and is why I like doing this stuff for a living. Funny how these things work out though -- it always turns out the the last thing that I try is the one that works. Go figure.

Even I am not a professional or i can say i am not nearly a programmer but I have researched on the internet, and tried a lot of ways to redirect to the page but couldn't succeed. I don't know how php works but i know somewhat how laravel framework is set up. i Know which file is where and which file do what. So if you tell me which file's code i should share with you, i can. or I can share All files code related to Login like LoginController, RedirectifAuthenticated, Register.php etc. Please help.

commented: Was this issue resolved as it is a week old? If not, there is an easy way with PHP, irrespective of using Laravel. +14

Peleg,

I have confidence that what needs to happen is the URL's referer on the signup page needs to be passed as a form variable to the confirmation PHP. The confirmation PHP page can then accept it and redirect to it. The problem is this does take custom coding, which might be out of the scope of what the OP is able to manage on their own right now.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.