Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

previous_url() not loading the base path together #2378

Closed
ChibuezeAgwuDennis opened this issue Oct 31, 2019 · 4 comments · Fixed by #2408
Closed

previous_url() not loading the base path together #2378

ChibuezeAgwuDennis opened this issue Oct 31, 2019 · 4 comments · Fixed by #2408

Comments

@ChibuezeAgwuDennis
Copy link

ChibuezeAgwuDennis commented Oct 31, 2019

Hello I had issue with the previous_url() helper when I try to use

redirect()->back()

It does not load the previous url with the base path. For example I was at http://localhost/scriptfolder/admin/auth/login
So when error trigger on form validation redirect back to the previous url as http://localhost/admin/auth/login. So I discovered that the complete url does not session with the scriptfolder together so I did some customization to the Core script which work for me by adding

$uri = str_replace('https://'. $_SERVER['HTTP_HOST'].'/', '', $uri);
$uri = str_replace('http://'. $_SERVER['HTTP_HOST'].'/', '', $uri);
$uri = (string) site_url($uri);

to the

vendor/codeigniter4/framework/system/CodeIgniter.php

to this

        /**
	 * If we have a session object to use, store the current URI
	 * as the previous URI. This is called just prior to sending the
	 * response to the client, and will make it available next request.
	 *
	 * This helps provider safer, more reliable previous_url() detection.
	 *
	 * @param \CodeIgniter\HTTP\URI $uri
	 */
	public function storePreviousURL($uri)
	{
		// Ignore CLI requests
		if (is_cli())
		{
			return;
		}
		// Ignore AJAX requests
		if (method_exists($this->request, 'isAJAX') && $this->request->isAJAX())
		{
			return;
		}

		// This is mainly needed during testing...
		if (is_string($uri))
		{
			$uri = new URI($uri);
		}
		
                // Here is where I edited to mine
		$uri = str_replace('https://'. $_SERVER['HTTP_HOST'].'/', '', $uri);
		$uri = str_replace('http://'. $_SERVER['HTTP_HOST'].'/', '', $uri);
		
		$uri = (string) site_url($uri);

                // End of my edit

		if (isset($_SESSION))
		{
			$_SESSION['_ci_previous_url'] = (string) $uri;
		}
	}

	//--------------------------------------------------------------------

which I know is not good practice so
I don't know if I should say I am suggesting touching the Codeigniter.php to this my edit or any other best method you wish

@ChibuezeAgwuDennis
Copy link
Author

@jim-parry I will like you to check this or any other person

@jim-parry
Copy link
Contributor

We have severak current/site_url issues being resolved, and this may well be related.
Fixing one thing triggers another problem, and I am still digging.
Patience, yound padawan!

@crustamet
Copy link
Contributor

yes, i have this issue too.. same previous url, i was digging to solve the problem but really i dont know exactly how codeigniter is setting the setPath, i tried to figgure it out myself but have no success.

this line of code from the incoming request class is overwhelming what it does xD
$this->uri->setPath($this->detectPath($protocol));

I think this needs to be rewritten or something. but yeah Codeigniter try to solve so many problems with only one fix so this is verry hard to do it only if treat all cases separately and not merged so people can understand what the code does.

@lonnieezell
Copy link
Member

Would seem to be related but it's not quite. The fix is simple, though: change line 376 of CodeIgniter.php to:

$this->storePreviousURL((string)current_url(true));

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging a pull request may close this issue.

4 participants