4

I just wrote a htaccess file and a simple rule.

RewriteRule ^/?([a-z]{2})/([0-9]{4})/?$ /run/run.php?country=$1&year=$2 [NC,L]
This does http://www.localhost/us/2014

On the php page, I accidently did:

echo $country.' '.$year;

This gave me the output below, which is correct.

us 2014

I did not do:

$country = $_GET['country'];
$year = $_GET['year'];

But it still worked. Is this normal behaviour. Can I use this for the rest of the rules and the site? I'm using WAMP on Windows 7 Home Premium.

jmenezes
  • 1,888
  • 6
  • 28
  • 44
  • 2
    Could it be that you unpack the $_GET array somewhere? This shouldn't work as far as I know, but I could be wrong. – Luc May 27 '13 at 12:43

2 Answers2

13

You might have turned on register_globals in php.ini. Thats why you are getting variable with the name of array index (Here $_GET). It is not a best practice to turn on register_globals.

You can read more here, why register_globals is bad.

Community
  • 1
  • 1
Nauphal
  • 6,194
  • 4
  • 27
  • 43
  • Will turning `register_globals` off prevent me from using `global` inside functions? Eg: function foo{ global $userName; echo $userName;}? – jmenezes May 27 '13 at 12:55
  • 2
    @jmenezes: No. It will not make any problem with the your global variables. – Nauphal May 27 '13 at 12:57
2

In my opinion this has nothing common with .htaccess. What PHP gets is the second part of ReWrite rule, so $_GET variables should be accessible. The server gets http://www.localhost/us/2014 and the PHP gets /run/run.php?country=us&year=2014

As nauphal wrote this might be (probably is) the register_globals issue.

Voitcus
  • 4,463
  • 4
  • 24
  • 40
  • It's on on my local machine, but not on the server. – jmenezes May 27 '13 at 12:50
  • Yes this is a server. When you type a `http://localhost/something` it is like your browser connects "outside" your computer with the server program, WAMP in this case. Both share the same PC. So your machine is a server and a client in the same moment, but different programs handle this. Browser is client, WAMP is server, and PHP is something that helps the server. – Voitcus May 27 '13 at 12:57
  • Actually, I meant `the server at the hosting company`. I was trying to say its `on` on my local machine, but it's `off` on the hosting server (the one thats with the hosting company). I just checked their php info file. – jmenezes May 27 '13 at 13:02
  • So your machine server has register_globals = on and the hosting company server has register_globals = off? So turn it off on your WAMP and it's ok. – Voitcus May 27 '13 at 13:08