Page 1 of 1

streamlining URL replacement code

Posted: Sat Oct 01, 2011 9:37 pm
by egg82
Well, I created a simple script to take anything written in the URL (?test=test&test2=test etc...) and POST it back to the page so it doesn't look ugly. It works, it's just very slow. I was just wondering if there's a way to streamline it so it'll be slightly faster. Any suggestions?

Here's the slow code :lol:

Code: Select all

<?php
$url = strip_tags(str_replace("/", "", $_SERVER['REQUEST_URI']));
if($url != ""){
	$url = str_replace("?", "", $url);
	$vars = explode("&", $url);
	$end = count($vars);
	echo($lang_header_wait);
	echo('<form action="http://'.$_SERVER['SERVER_NAME'].'/" method="post" id="redir">');
	for($i=0;$i<$end;$i++){
		$var = explode("=", $vars[$i]);
		echo('<input type="hidden" name="'.$var[0].'" value="'.$var[1].'">');
	}
	?>
    </form>
    <script language="JavaScript" type="text/javascript">
    <!--
    document.getElementById('redir').submit();
    //-->
    </script>
    <?
	exit();
}
Again, any help at all would be great. Thanks!

Re: streamlining URL replacement code

Posted: Sat Oct 01, 2011 10:20 pm
by egg82
made it faster. The

Code: Select all

document.getElementById('redir').submit();
was within a 10 second setTimeout function. Fixed it in the above post. Still, if it could be compressed, that would be great as well.

(by the way, it will work on any server. Just copy+paste)

Re: streamlining URL replacement code

Posted: Sat Oct 01, 2011 10:40 pm
by twinedev
Ok, so you have a link to something like:

http://www.domain.com/?test1=whatever&test2=whoever&test4=whichever

and then you are wanting it to actually call back to:

http://www.domain.com/ with all those values in $_POST instead of $_GET correct?

You may have already considered these, and you may be ok with them, but just wanted to point out the following if you haven't:

1. Your method required the user to have javascript, while these days, not such a big thing, as by default most browsers have it and it is enabled (even mobile browsers), it is good not to rely on something Client side. Also see #3
2. People will not be able to bookmark these pages, as they will be bookmarking the home page without the data being passed to it.
3. Also, if you have any concern for SEO, Search engines will see the content on all the pages using this as just a form. They will not post and follow the form on to the actual data.
4. Navigation (being hitting back) will be a pain. When they hit back it will technically take them back to the form which will resubmit again.

So all that being said, if you still need something to do this, here is a solution that solves #1 above #2,3,&4 without having the final page being a unique URL where you do not have to kick them from one URL to the other, you won't be able to avoid.

index.php:

Code: Select all

<?php

	session_start();
	if (count($_GET)>0) {
		$_SESSION['getdata'] = $_GET;
		header('Location: /');
		exit;
	}

	if (isset($_SESSION['getdata'])) {
		if (is_array($_SESSION['getdata'])) { $_GET = $_SESSION['getdata']; }
		unset($_SESSION['getdata']);
	}

	// Continue on with regular script
Once you get past this, $_GET will be populated just as if you didn't use something like this.

-Greg

Re: streamlining URL replacement code

Posted: Sat Oct 01, 2011 10:54 pm
by egg82
Nicely done. Good use of the header, and the code is both simple and quick. However, I do have a question:
Why are you checking is_array when $_GET always returns an array? Isn't that a bit redundant?

Re: streamlining URL replacement code

Posted: Sat Oct 01, 2011 11:51 pm
by twinedev
Just as a safeguard in case you were to ever have some other code that sets $_SESSION['getdata'] by mistake. This assumes that it is being set, redirecting back to itself, reading it, and then unsetting that session variable. Consider if something accidentally sets it and then the user clicks on a link to the home page. Just my obsessiveness with trying to protect things ;-)

-Greg