Jump to content

getting $_GET in a framework


Destramic

Recommended Posts

hey guys im having a bit of trouble getting $_GET in my framework. now the ony way to get query from a url such as:

 

 

users/activate-account?email_address=test&token=test

 

 i have to use $_SERVER['REQUEST_URI'] unless im doing something wrong?

 

 

[REQUEST_URI] => /bisi/user/activate-account?email_address=test&token=test

 

by doing this:

print_r($_GET);

all i get is :

 

 

Array ( [uri] => user/activate-account )

 

just concerned im doing something wrong?...any advise on this please guys.

 

was thinking of doing something like this in my request

public function get_query($name = null)
	{
		$uri = $_SERVER['REQUEST_URI'];
	
		if (parse_url($uri, PHP_URL_QUERY))
		{
			$query = explode("?", $uri);
			$query = explode("&", $query[1]);
					
			$array = array();
				
			foreach ($query as $string)
			{
				$string = explode("=", $string);
				$query_name   = $string[0];
				$query_value  = $string[1];			
				$array[$query_name] = $query_value;
// $_GET[$query_name] = $query_value; possibly?
			}
				
			if ($name !== null &&
		        array_key_exists($name, $array))
			{
				return $array[$name];
			}
			
			return $array;
		}
		
		return null;
	}

thanks

 

 

Link to comment
https://forums.phpfreaks.com/topic/293219-getting-_get-in-a-framework/
Share on other sites

The values in $_GET is strange, no doubt. Perhaps spending some time trying to understand why that is happening.

 

In the meantime, this may be a (less-than-ideal) work-around:

$tmp1 = explode("?", $uri); // should give array([0] => '/bisi/user/activate-account', [1] => 'email_address=test&token=test')
$tmp2 = explode("&",$tmp1[1]); // should give array([0] => 'email_address=test', [1] => 'token=test')
foreach($tmp2 as $querystringKeyVal) {
etc.

this is what im using...is there a way of doing things better here to get $_GET as it should be?

Options +FollowSymlinks
RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ index.php?uri=$1 [PT,L]

thanks

That rewrite rule removes the need for typical GET parameters by making your urls "pretty".

 

So instead of this:

 

users/activate-account?email_address=test&token=test
You would use something more like:

 

users/activate-account/test/test
Of course then you need some sort of "router" to parse and handle these parameters for you.

 

If this is your own framework you need to decide how your urls are going to be formed.

The point I'm trying to make is why have the rewrite at all if you're just going to use normal querystring parameters? I understand you want to force everything through a front controller, but why stop there?

 

But yeah, you could just use the QSA flag to have apache append existing parameters.

 

RewriteRule ^(.*)$ index.php?uri=$1 [PT,L,QSA]

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.