Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations SkipVought on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Redirect Question - masking url in address bar 1

Status
Not open for further replies.

buspass

Programmer
Feb 2, 2003
29
0
0
GB
There may not be a php solution to this - but I will explain what I want to do first.

I want to be able to show a link in an article resource box that points to my root domain with a variable in the querystring - eg "
When clicked, my php script will $_GET["id"] the value of id and then open a text file to read the "page.html" on my domain that is associated to that id.

So id=1 might be cats.html, id=2 might be dogs.html and so on.

The idea is that I can show an appropriate page which will appear to be on home page. So I would need that to always appear in the address bar.

I can get it to work so the appropriate page is displayed but I always get in the address bar regardles of the page I am displaying.

Is there anyway to change the appearance of the url in the address bar so it loses the ?id=123 bit?

I have tried frames but cannot get it to work?

Any help and code snippet appreciated

Here's some code so far: I haev a text file id_list.txt to store page data in format 1,page1.html, 2,page2.html, etc

Code:
<?php

$id = $_GET["id"];
$root_domain =  $_SERVER['HTTP_HOST'];
$filename = "id_list.txt"; // this is where i store the html page names e.g. 1, page1.html, 2,page2.html etc, comma delimited
$fd = fopen ($filename, "r");
$contents = fread ($fd,filesize ($filename));
fclose ($fd);
$delimiter = ",";
$splitcontents = explode($delimiter, $contents);
//echo"<p>file $id = $splitcontents[$id]</p>";
?>
<HTML><HEAD>
<META NAME="description" CONTENT="<?=$root_domain;?>">
<META NAME="keywords" CONTENT="">
</HEAD>
<FRAMESET border=0 rows="100%,*" frameborder="no"
marginleft=0 margintop=0 marginright=0 marginbottom=0>
<frame src="<?=$splitcontents[$id];?>" scrolling=auto frameborder="no" border=0 noresize> <frame topmargin="0" marginwidth=0 scrolling=no marginheight=0
frameborder="no" border=0 noresize>
</FRAMESET>
</HTML>
 
look at mod rewrite.

if you want a php only solution take the following steps:

1. open a session.
2. read the querystring and store in a session
3. redirect to 4. start session
5. check for presence of querystring in session.
6. if exists read relevant page and either delete or keep the relevant session variable based on relevant middle-layer logic.
 
This is usually done from the web server.

In the case of Apache you can use mod_rewrite. Other servers will have other methods of doing this, so you'll have to find out what web server you are going to be running this on.


----------------------------------
Phil AKA Vacunita
----------------------------------
Ignorance is not necessarily Bliss, case in point:
Unknown has caused an Unknown Error on Unknown and must be shutdown to prevent damage to Unknown.
 
Sessions are a good idea - I will play around with some code.

Whatever solution I (we) come up with, it has to be portable across different servers, and i don't want to use htaccess at all.

 
i'm not an SEO specialist but i can see why search spiders might not like a redirect solution. perhaps this can be worked around by sending a moved temporarily header.

i can't see how users might be confused unless one expressly wants to permit deep linking.

however i can't see anything wrong with a querystring being visible in the address bar of a browser. users are used to this.

as a third possible solution the OP could do the following

1. use javascript to bind all links to an ajax request and disable the default action
2. write the response to a content div (thus refreshing the page).
3. rebind the new page links (as the old bindings will have been lost by the overwrite of a content div)

using this method, search engines (not being js aware) will visit the true link and SEO will be maintained whereas the true users will stay on the base url.

 
Hi

jpadie said:
i can see why search spiders might not like a redirect solution. perhaps this can be worked around by sending a moved temporarily header.
Moved where ? Behind the distinct URLs there will be no actual content.

Beside that, delivering completely different content each time for the same URL, the search engines may think there is some cloaking and blacklist the site.
jpadie said:
i can't see how users might be confused unless one expressly wants to permit deep linking.
[ul]
[li]Bookmarking a page will not work, only bookmarking the link which led to that page.[/li]
[li]Page URL sent in mail/messenger will not work, people will have to send the link's href.[/li]
[li]Search engine results will link to a page unrelated to the searched terms.[/li]
[li]Backward/forward navigation will not work, maybe even page reload will be affected.[/li]
[/ul]


Feherke.
 
The purpose of this script is simply to allow me to include links containing querysrings in my articles. Some article directories do not allow linking to sub domains or sub folders on your domain, so they won't allow or sub.site.com for example. So all I am after is displaying an appropriate page on my domain based upon the id passed to the home page via the querystring.

The address bar must not show the querystring, it has to show the root domain Anything else will be disallowed by the directories. I agree users aren't that concerned with what's in the address bar, but this is specifically for the directories.

As far as indexing goes, I don't know what would happen - will have to try and monitor. I am not too worried about a user bookmarking the redirected page and getting lost later. I could display a simple page on the root containg links to each article or something like that.

@jpadie - your solution sounds interesting but I don't have skill to code it.
 
the ajax solution will not work if the links are not on your site.

the session based solution can be coded like this (i am making assumptions on your table and column names).
table name: articles
cols: id (primary key)
title
excerpt
fulltext


i am using the POST method within this example and i am also using buttons. if you would rather have the buttons look like links it is easy enough to restyle them with php.

i trust it is obvious that i have not employed the ajax method that i was talking about above in this site. this can be done but the POST method works equally.

feherke's quoted problems are not easily surmountable but the showPreviousVisitedArticles() function i have included goes some way to fixing the back/forward navigation issues.

Code:
<?php
session_start();
if (empty($_SESSION['id'])) $_SESSION['id'] = array();

if (isset($_GET['id'])){
	addToSession($_GET['id']);
	session_write_close();
	header('Location: [URL unfurl="true"]http://'.$_SERVER[/URL]['SERVER_NAME']);
	exit;
} elseif (!empty($_POST['id'])){
	addToSession($_POST['id']);
	showArticle($id);
} else {
	$id = (count ($_SESSION['id']) > 0) ? end($_SESSION['id']) : 0;
	if ($id === 0){
		showListOfArticles();
	} else {
		showArticle($id);
	}
}

function addtosession($id){
	$max = 10; //maximum number stored
	if (FALSE!== ($cur = array_search($id, $_SESSION['id']))){
		unset ($_SESSION['id'][$cur]);
		reset ($_SESSION['id']);
	}
	if (count($_SESSION['id'] >= $max)){
		unset($_SESSION['id'][0]);
		reset($_SESSION['id']);
	}
	$_SESSION['id'][] = $id;
}

function showListofArticles(){
	dbConnect();
	$sql = "select id, title, excerpt from articles";
	$result = mysql_query($sql);
	echo "<dl>\r\n";
	while ($row = mysql_fetch_object($result)){
		$row->excerpt = nl2br(htmlspecialchars($row->excerpt));
		echo <<<HTML
		
	<dt><form action={$_SERVER['PHP_SELF']} method="post"><button name="id" value="{$row->id}">{$row->title}</button></form></dt>
	<dd>{$row->excerpt}</dd>

HTML;
	}
	echo "\r\n</dl>";
	
	echo showPreviousVisitedArticles();
}

function showArticle($id){
	dbConnect();
	$sql = "select id, title, fulltext from articles where id='%s'";
	$query = sprintf($sql, trim(mysql_real_escape_string($id)));
	$result = mysql_query($query);
	$row = mysql_fetch_object($result);
	$row->fulltext = nl2br(htmlspecialchars($row->fulltext));
	echo <<<HTML
<h2>{$row->title}</h2>
<div class="bodyContent">
	{$row->fulltext}
</div>
HTML;
	echo showPreviousVisitedArticles();
}

function showPreviousVisitedArticles(){
	$return = <<<HTML

<div id="previousVisited">
<h4>Previously Viewed</h4>
<ul>

HTML;
	foreach ($_SESSION['id'] as $id){
		$sql = "Select title from articles where id='%s'";
		$query = sprintf($sql, mysql_real_escape_string($id));
		$result = mysql_query($query);
		$row = mysql_fetch_object($result);
		$return .= <<<HTML

	<li><form action="{$_SERVER['PHP_SELF']}" method="post"><buton type="submit" name="id" value="$id">{$row->title}</button></form></li>

HTML;
	}
	$return .= "\r\n</ul></div>";
	return $return;
}
function dbConnect(){
	mysql_connect($server, $username, $password);
	mysql_select_db($database);
}
?>
 
Thanks for all the input.

jpadie , thanks for that code but I have got this working without need for database. I wanted a simple portable solution and just use a text file to store the article page names etc.

I used a session as you origianlly suggested and it works a treat.

I will test this over next few week to monitor any adverse effect in SE's.

Thanks again to all.
 
you can use the code i wrote above without any database. the concepts are entirely transferrable to other storage methods (flat file).

i thought you said that you were not able to code the solution. hence i did it for you.

you might take advantage of some of the ideas in the code for navigation in any event.
 
It was the Ajaxy stuff that put me off :)
but will try your code as well.

Actually just trying my code with the domain.com straihgt into the address bar (or via a bookmark) and I am getting a Redirect Loop error.

I must have something wrong with my code.

This is my code attempt

Code:
<?php

session_start();  
$id = $_GET["id"];
if (!(isset($_SESSION['id']))){
    $_SESSION['id'] = $id;
	header("location:[URL unfurl="true"]http://site.com");[/URL]

}else{

	if (!(isset($_SESSION['id']))){
		// Tried to add real content here to display should user access domain direct
		echo"<p> Some defauult content here...</p>";
		exit;
		
		}else{
			$id = $_SESSION['id']; 
			$root_domain =  $_SERVER['HTTP_HOST'];
			$filename = "id_list.txt"; // this is where i store the html page names e.g. 1, page1.html, 2,page2.html etc, comma delimited
			$fd = fopen ($filename, "r");
			$contents = fread ($fd,filesize ($filename));
			fclose ($fd);
			$delimiter = ",";
			$splitcontents = explode($delimiter, $contents);
			session_destroy();
		?>

		<HTML>
		<HEAD>
		<META NAME="description" CONTENT="">
		<META NAME="keywords" CONTENT="">
		</HEAD>
		<FRAMESET border=0 rows="100%,*" frameborder="no"
		marginleft=0 margintop=0 marginright=0 marginbottom=0>
		<frame src="<?=$splitcontents[$id];?>" scrolling=auto frameborder="no" border=0 noresize> <frame topmargin="0" marginwidth=0 scrolling=no marginheight=0 frameborder="no" border=0 noresize>
		</FRAMESET>
		</HTML>

		<?

		}

}

?>

Can you see the problem?
 
you are testing for the presence of $_SESSION['id'] twice. the second test is inside that part of the first test in which you KNOW that the variable exists. and yet you are only doing something in the event that the variable does not exist.

i really really really would not use frames. they are awful things and are never necessary. a simple 'include' here would be much better instead of frames.

Code:
$splitcontents = explode($delimiter, $contents);
include $splitcontents[$id];
exit;
 
Is this what you meant me do?

Code:
<?php

session_start();  
$id = $_GET["id"];
if (!(isset($_SESSION['id']))){
    $_SESSION['id'] = $id;
	header("location:[URL unfurl="true"]http://audiovoiceads.info");[/URL]

}else{

			$id = $_SESSION['id']; 
			$root_domain =  $_SERVER['HTTP_HOST'];
			$filename = "id_list.txt"; // this is where i store the html page names e.g. 1, page1.html, 2,page2.html etc, comma delimited
			$fd = fopen ($filename, "r");
			$contents = fread ($fd,filesize ($filename));
			fclose ($fd);
			$delimiter = ",";
		$splitcontents = explode($delimiter, $contents);
		include $splitcontents[$id];
		exit;

	

}

?>

If so, this doesn't work when I try it - I still se the ?id=123 in the addres bar.

Will have a look through you code to see if I can substitue a flat file instead of the DB
 
yes. although i would test primarily for $_GET['id'] rather than for $_SESSION['id']

Code:
<?php
session_start();  
if (isset($_GET['id'])){
	$_SESSION['id'] = $_GET['id'];
	session_write_close(); //to avoid race conditions
	header("location:[URL unfurl="true"]http://audiovoiceads.info");[/URL]
	exit;
}elseif(!empty($_SESSION['id'])){

	$id = &$_SESSION['id']; 
	$root_domain =  $_SERVER['HTTP_HOST'];
	$filename = "id_list.txt"; // this is where i store the html page names e.g. 1, page1.html, 2,page2.html etc, comma delimited
	$contents = file_get_contents($filename);
    $splitcontents = explode(",", $contents);
    include $splitcontents[$id];
    exit;
} else {
	//do some default action
}
 
Yes that is very nice.

Solves the loop problem - thank you

much better than a frame

Thanks added again :)
 
This script works nicely for files I host myself, and I am using it.

I would like to expand its use to include URLs hosted elsewhere if possible - can that be done?

So, say I want to have my affiliate offer page appear to a user and retain my root domain in the address bar - can I do that without frames adapting the above php?

So an article of mine might have a link and when clicked it takes user to but the content will be an affilaite offer page or something like that. The affiliate page might have a url of or something like that and you can't have links like that in articles.

Thanks
 
not that I am aware of. you would be better to use cURL in this case. you would then have to rewrite every link on the page to refer back to your domain and append the remote link as a query string.
 
cURL - Would you be able to provide a code snippet to point me in the right direction?

Thanks
 
it's a really bad idea to do this.

what are the business rules that mandate that you keep the url unknown to the users?
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top