zero-perfoliate
zero-perfoliate

Author Topic: (Beginer) How to fight $_GET variable stacking in url ?  (Read 674 times)

Offline arma

  • PHP Workers
  • **
  • Posts: 5
  • Karma: +0/-0
(Beginer) How to fight $_GET variable stacking in url ?
« on: December 03, 2009, 04:41:10 AM »
Hello fellas.

First of all i was using search but since i don't know how to qualify this i couldn't find any answer.

So i have made my first php website with closed access to it for people managing stuff. Anyway thats not important, but where problem is for me that i have links on page languages.
Each language points to
Code: [Select]
'<a href="' . $_SERVER['PHP_SELF'] . '?lang=en">' if there was no $_GET['lang'] key and
Code: [Select]
'<a href="' . $_SERVER['REQUEST_URI'] . '&lang=en">' if there was $_GET['lang'] key. So problem is if user click same language again and again and again he gets nasty url in browser something like this index.php?lang=en&lang=en&lang=en&lang=en and so on :) Im not sure what should i do for this matter. Altho all works it's just i don't want to be so user non-friendly about urls.

Thanks for reading this.
Best regards Andrew.

Offline hirley0

  • PHP Workers
  • **
  • Posts: 20
  • Karma: +0/-0
Re: (Beginer) How to fight $_GET variable stacking in url ?
« Reply #1 on: December 03, 2009, 08:20:12 AM »
20091203 10:20 csT again my guess is it would help to include a link to the site /-/Ub'

http://web.pdx.edu/~pdx00782 click , it does not work IT has been hacked way often Xct

Offline arma

  • PHP Workers
  • **
  • Posts: 5
  • Karma: +0/-0
Re: (Beginer) How to fight $_GET variable stacking in url ?
« Reply #2 on: December 03, 2009, 12:09:56 PM »
I'm working on localhost don't have website to link to.
Code is fairly basic i belive $_GET key stacking is standart php stuff just need to know how to not inlude same key over and over in website url.

here is code of those links if it might help.

Code: [Select]
if(empty($_GET['lang'])){
  echo '<a href="' . $_SERVER['PHP_SELF'] . '?lang=en"><img src="/static/pb_tracker/img/lang/en.png" /></a>';
  echo '<a href="' . $_SERVER['PHP_SELF'] . '?lang=ru"><img src="/static/pb_tracker/img/lang/ru.png" /></a>';
  echo '<a href="' . $_SERVER['PHP_SELF'] . '?lang=lv"><img src="/static/pb_tracker/img/lang/lv.png" /></a>';
}else{
  echo '<a href="' . $_SERVER['REQUEST_URI'] . '&lang=en"><img src="/static/pb_tracker/img/lang/en.png" /></a>';
  echo '<a href="' . $_SERVER['REQUEST_URI'] . '&lang=ru"><img src="/static/pb_tracker/img/lang/ru.png" /></a>';
  echo '<a href="' . $_SERVER['REQUEST_URI'] . '&lang=lv"><img src="/static/pb_tracker/img/lang/lv.png" /></a>';
}


hotnoob

  • Guest
Re: (Beginer) How to fight $_GET variable stacking in url ?
« Reply #3 on: December 03, 2009, 12:24:52 PM »
try using $_SERVER['SCRIPT_NAME'] instead.
it should not include the variables in it.

Offline arma

  • PHP Workers
  • **
  • Posts: 5
  • Karma: +0/-0
Re: (Beginer) How to fight $_GET variable stacking in url ?
« Reply #4 on: December 03, 2009, 04:49:21 PM »
$_SERVER['SCRIPT_NAME'] strips all of my $_GET keys in url it defaults to index.php no matter how many variables was there.

Maybe i should reclarify. I have many links that gets sent to $_GET when user click on link.
For example i have ?page=1 and ?lang=en and ?status=all etc.
If you go to index.php?page=1&lang=en&status=all for example after that if you press any of those links that already has been used it will add up to the url so if we click on language english again we get index.php?page=1&lang=en&status=all&lang=en notice how it's getting stacked up instead of changing existing one.

hotnoob

  • Guest
Re: (Beginer) How to fight $_GET variable stacking in url ?
« Reply #5 on: December 03, 2009, 05:59:17 PM »
Ohh!

k, ive got an old script... it was part of one of the first websites i ever made...

Code: [Select]
$newlocation = 'http';
  if ($_SERVER["HTTPS"] == "on")
{
$newlocation .= "s";
}
  $newlocation .= "://";
  $newlocation .= $_SERVER['HTTP_HOST'] . $_SERVER['PHP_SELF'];
$newlocation .= "";
                                $valCount =0;

foreach ($_GET as $key => $val)
{
$newlocation .="&$key=$val";
}

Then you add this <a href=<?php echo $newlocation;?>">
and if you want to filter out some of the keys and suff or wat ever, just add some if statements or what ever to the foreach loop.

hotnoob

  • Guest
Re: (Beginer) How to fight $_GET variable stacking in url ?
« Reply #6 on: December 03, 2009, 06:12:12 PM »
Errr made a mistake with that last bit...

Instead of wat ever... use '<a href="' . $newlocation . '?lang=en">'
instead.

Offline arma

  • PHP Workers
  • **
  • Posts: 5
  • Karma: +0/-0
Re: (Beginer) How to fight $_GET variable stacking in url ?
« Reply #7 on: December 04, 2009, 01:20:58 AM »
thanks hotnoob i'll mess around with that code.