Global vars in a class.
Moderator: General Moderators
Global vars in a class.
Hi all,
Is it possible to use global, pre-defined vars like $_SESSION["blahvar"] or $_SERVER["REQUEST_URI"] without actually *passing* them into the class itself?
As an example:
# Method
function cameFrom()
{
$this->cameFrom = urlencode($SERVER["HTTP_REFERER"]);
return $this->cameFrom;
} # End method
Thanks in advance,
Mike Pearce
Is it possible to use global, pre-defined vars like $_SESSION["blahvar"] or $_SERVER["REQUEST_URI"] without actually *passing* them into the class itself?
As an example:
# Method
function cameFrom()
{
$this->cameFrom = urlencode($SERVER["HTTP_REFERER"]);
return $this->cameFrom;
} # End method
Thanks in advance,
Mike Pearce
$_SESSION, $_SERVER and some more are super(or auto-)global arrays that can be accessed from anywhere.
http://www.php.net/manual/en/reserved.variables.php
http://www.php.net/manual/en/reserved.variables.php
Thanks for the quick reply volka.
That's what I though, although that function doesn't return anything, neither does it return anything if I use REMOTE_ADDR.
If I do: $this->cameFrom = "testvarcontent"; it returns the string ok.
What could it be? I've been looking round the web and not found a thing.
Thanks,
Mike
That's what I though, although that function doesn't return anything, neither does it return anything if I use REMOTE_ADDR.
If I do: $this->cameFrom = "testvarcontent"; it returns the string ok.
What could it be? I've been looking round the web and not found a thing.
Thanks,
Mike
Thanks,
That was a typo in the post, in the actual class I have:
That was a typo in the post, in the actual class I have:
Code: Select all
function cameFrom()
{
$this->cameFrom = urlencode($_SERVERї"REQUEST_URI"]);
#$this->cameFrom = urlencode($this->requestURI);
return $this->cameFrom;
} # End functionwhich version of php do you use?
if you make itwhat does it print?
if you make it
Code: Select all
function cameFrom()
{
echo '<pre>'; print_r($_SERVER); echo '</pre>'; // debug only, do not forget to remove
$this->cameFrom = urlencode($_SERVER["REQUEST_URI"]);
#$this->cameFrom = urlencode($this->requestURI);
return $this->cameFrom;
} # End function$HTTP_SERVER_VARS is not superglobal. If you want to use it within the scope of a function or method you have to import the global scope for that variable
Code: Select all
function cameFrom()
{
global $HTTP_SERVER_VARS;
echo '<pre>'; print_r($HTTP_SERVER_VARS); echo '</pre>'; // debug only, do not forget to remove
$this->cameFrom = urlencode($HTTP_SERVER_VARS["REQUEST_URI"]);
#$this->cameFrom = urlencode($this->requestURI);
return $this->cameFrom;
} # End function