Jump to content

Automatic Redirection - Need Help.


filteredhigh

Recommended Posts

I've created a logging script for a website I'm building but I would like to add a feature to it, if possible. Basically I'd like to be able to automatically redirect the user back 2 pages, in theory the last page they were looking at. I've been searching for an answer for hours now and although i can find stuff in relation to redirection, I've found nothing to suit my particular issue.

 

Here's a sippet of what I'm currently using to redirect my user, but obviously it takes them to the home page only.

 

	//Start defining the URL.
	$url ='http://' . $_SERVER['HTTP_HOST'] . dirname($_SERVER['PHP_SELF']);
	//Check for a trailing Slash.
	if ((substr($url, -1) =='/') OR (substr($url, -1) == '\\')) {
		$url = substr ($url, 0, -1);//Chop off the slash.
	}
		// Add the page.
		$url.='/home.php';
		ob_end_clean(); //Delete the buffer
		header("Location: $url");
		exit(); //quit the script

		} 

 

 

I thought the solution might be with the "history.go(-2)" command so I tried this.

	$url = history.go(-2);
		ob_end_clean(); //Delete the buffer
		header("Location: $url");
		exit(); //quit the script

but it didn't work. I suspect I'm missing something obvious but can't figure it out. Would really appreciate import on this issue.

 

 

Link to comment
https://forums.phpfreaks.com/topic/101365-automatic-redirection-need-help/
Share on other sites

use this code on all your user pages (pages that you may be redirecting back to):

<?php
session_start();
$host = $_SERVER['HTTP_HOST'];
$self = $_SERVER['PHP_SELF'];
$query = !empty($_SERVER['QUERY_STRING']) ? $_SERVER['QUERY_STRING'] : null;
$url = !empty($query) ? "http://$host$self?$query" : "http://$host$self";
$_SESSION['history'] = $url;


/* Place rest of page below */

Now, when you want to redirect them back:

 

<?php
session_start();
/* Your stuff here, without any output, or you can't do a header redirect */

header("location:".$_SESSION['history']);

Thanks jonsjava, seems like a good idea, but afraid doesn't work for me(or my site). Maybe is I explain in detail it will help a solution unfold.

 

Firstly I will mention that the site uses a include:header.php and a include:footer.php. Which makes it hard to add things to the header of a specific page/file. My site is a large site with 50+ pages(and will grow) so am unwilling to code each individual page.

 

What I'm trying to accomplish is:

I have a recommendations selection on my website, where register users can add a comment, but only once they logon. Say one of my users wants to be able to make a comment but needs to log on first, I want the site to be able to redirect back to their recommendation after they've logged on, which is two pages back, once they have logged on. The login page is currently a separate page.

 

I thought the "history.go(-2);" would work nicely but I need to be able to trigger it automatically rather than have the user press a button. Is there a way of doing this.

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.