How do I pass a PHP variable (eg. $_GET)?

Discussion forum for MOD Writers regarding MOD Development.
Locked
Markstar
Registered User
Posts: 72
Joined: Mon May 01, 2006 6:26 pm

How do I pass a PHP variable (eg. $_GET)?

Post by Markstar »

Hi,
within my website, I thought I'd give users a choice whether or not they would like to use JavaScript+Flash or not. To make this work, pass a variable to each page whether JavaScript and Flash should be enabled or not. Currently I do that with $_GET, like this: http://www.url.com?flash=y (or 'n').

Now, since I would like to keep this functionality for the forum as well, I obviously need some way to make this work there, so any help on how I can accomplish this would be greatly appreciated! I'm just beginning with the design of my website, so I can still easily change the system if you have a better idea. :P

Thank you in advance!

Cheers


P.S.: Of course I also make use of <noscript>, but as I said, I would like to give visitors an explicit choice in addition to automatically detecting what they have.
User avatar
Brf
Support Team Member
Support Team Member
Posts: 52105
Joined: Tue May 10, 2005 7:47 pm
Location: {postrow.POSTER_FROM}
Contact:

Re: How do I pass a PHP variable (eg. $_GET)?

Post by Brf »

phpBB uses a lot of GET variables, as you can see by looking at your webbrower's addressbar. I guess I do not understand what exactly your question is.
Markstar
Registered User
Posts: 72
Joined: Mon May 01, 2006 6:26 pm

Re: How do I pass a PHP variable (eg. $_GET)?

Post by Markstar »

Brf wrote:phpBB uses a lot of GET variables, as you can see by looking at your webbrower's addressbar. I guess I do not understand what exactly your question is.
Yes, it does, but I don't know how to pass my own variable along with it.

Let's say, for example, that my forum location is http://www.url.com/forum/index.php. From my website, I could refer to that location with a variable, eg. http://www.url.com/forum/index.php?flash=t . However, as soon as I click on something, that variable is lost because phpbb does not pass is along.

And I would like to learn how to do exactly that, either via _GET or some other way, I don't care as long as it works.

Edit: I found the following advice here:
To retrieve a value from $_POST/$_GET you should always use phpBB3's request_var function. You should not access $_POST/$_GET directly. An exception is checking whether a variable is set, such as isset($_POST['submit']).
However, the link they give to the request_var function does not work. It did lead me to the phpbb wiki, but I must admit that I don't know how I can use that information to solve my problem. :oops:
User avatar
Brf
Support Team Member
Support Team Member
Posts: 52105
Joined: Tue May 10, 2005 7:47 pm
Location: {postrow.POSTER_FROM}
Contact:

Re: How do I pass a PHP variable (eg. $_GET)?

Post by Brf »

You use request_var() to get the contents of your URL parameter.

If you want to preserve the parameter, then you should retrieve it in the session functions, and stuff it in the $_EXTRA_URL variable. Whatever is in there is tacked in the URL in append_sid.

Refer to the way the style parameter is passed in includes/session.php around line-1580.
Markstar
Registered User
Posts: 72
Joined: Mon May 01, 2006 6:26 pm

Re: How do I pass a PHP variable (eg. $_GET)?

Post by Markstar »

Brf wrote:Refer to the way the style parameter is passed in includes/session.php around line-1580.
Thank you for your help! You mean this part, right?

Code: Select all

		if (!empty($_GET['style']) && $auth->acl_get('a_styles') && !defined('ADMIN_START'))
		{
			global $SID, $_EXTRA_URL;

			$style = request_var('style', 0);
			$SID .= '&style=' . $style;
			$_EXTRA_URL = array('style=' . $style);
		}
		else
		{
			// Set up style
			$style = ($style) ? $style : ((!$config['override_user_style']) ? $this->data['user_style'] : $config['default_style']);
		}
You use request_var() to get the contents of your URL parameter.

If you want to preserve the parameter, then you should retrieve it in the session functions, and stuff it in the $_EXTRA_URL variable. Whatever is in there is tacked in the URL in append_sid.
Sadly I have no idea how to do that. :oops: To keep it simple, my flash variable can be '0' or '1'. Is it correct that to "pull" the variable, I use

Code: Select all

$flash = request_var('flash', 1);
, meaning that 1 is the default value? I also changed:

Code: Select all

$_EXTRA_URL = array('style=' . $style);
to

Code: Select all

$_EXTRA_URL = array('flash=' . $flash . 'style=' . $style);
. However, that did not work (after clearing the cache, etc) - the variable is not being passed. I guess I've made a mistake, but I have no idea what to do there, so I would really appreciate it if you could nudge me in the right direction. ;)
User avatar
Brf
Support Team Member
Support Team Member
Posts: 52105
Joined: Tue May 10, 2005 7:47 pm
Location: {postrow.POSTER_FROM}
Contact:

Re: How do I pass a PHP variable (eg. $_GET)?

Post by Brf »

First, that particular piece of code is only executed if there is a style parameter on the URL. You would have to add your own line of code. Second, $_EXTRA_URL is an array. You would add your parameter as another cell in that array.

Like this:

Code: Select all

$flash = request_var('flash', 0);
if ($flash)
{
    $_EXTRA_URL[] = 'flash=' . $flash;
}
User avatar
Nelsaidi
Registered User
Posts: 525
Joined: Mon Feb 11, 2008 1:59 pm
Location: London, UK
Contact:

Re: How do I pass a PHP variable (eg. $_GET)?

Post by Nelsaidi »

Err, I would suggest the use of cookies. phpBB has some helper functions - however I'm not to aware of them, instead.

Havea a look at -> http://uk2.php.net/setcookie
Image
Click here to find out what eRepublik is.
Markstar
Registered User
Posts: 72
Joined: Mon May 01, 2006 6:26 pm

Re: How do I pass a PHP variable (eg. $_GET)?

Post by Markstar »

Brf wrote:First, that particular piece of code is only executed if there is a style parameter on the URL. You would have to add your own line of code. Second, $_EXTRA_URL is an array. You would add your parameter as another cell in that array.

Like this:

Code: Select all

$flash = request_var('flash', 0);
if ($flash)
{
    $_EXTRA_URL[] = 'flash=' . $flash;
}
Sorry again for being so stupid, but I don't get the logic of the original part of the code, and since just inserting your code after if (!empty($_GET['style']) && ... did not work, I would be very thankful if you could correct me on my probably wrong assumptions:

Code: Select all

if (!empty($_GET['style']) && $auth->acl_get('a_styles') && !defined('ADMIN_START'))
1) Doesn't $_GET['style'] already ask for the variable from the URL and checks whether or not it exists (if not, then the clause does not get executed)?
2) I don't know what the second and third conditions mean, but I assume they are there to make sure styles are allowed?

Code: Select all

global $SID, $_EXTRA_URL;
3) Creating two global variables? So that makes me assume that I either need to include the code about my flash variable in that if clause (otherwise $_EXTRA_URL will not be created), or I also have to create it in my own if-clause.

Code: Select all

$style = request_var('style', 0);
4) So again...if this is the proper way to retrieve variables from the URL ($_GET variables), why is $_GET being used in the if-clause?

Code: Select all

$SID .= '&style=' . $style;
5) Assigning the style to SID. Don't know what that means, but maybe I don't need to know. :P

Code: Select all

$_EXTRA_URL = array('style=' . $style);
6) Assign an array to the _EXTRA_URL variable (so that 'style=' . $style can be appended to the URL).

I feel I'm so close to the solution, but as I said, I don't understand the code so I don't know how and where to put in my own code. :oops:


@Nelsaidi: Thank you for the suggestion. Indeed, cookies might be a solution as well, but I would prefer it to do this way (it's more transparent to the user and many people are suspicious of cookies). Also, I have no experience with cookies, even though I admit I'm a beginner concerning PHP as well. :roll:
User avatar
T0ny
Registered User
Posts: 1383
Joined: Sun Jan 29, 2006 8:42 pm
Location: Lancashire
Name: Tony

Re: How do I pass a PHP variable (eg. $_GET)?

Post by T0ny »

Pretty much all the code you need is already there for you to copy, you just need to make the setting of $_EXTRA_URL conditional on whether it is already an array

Code: Select all

		if (!empty($_GET['flash']))
		{
			global $SID, $_EXTRA_URL;
			
			$flash = request_var('flash', 0);
			$SID .= '&flash=' . $flash;
			
			if (is_array($_EXTRA_URL))
			{
				$_EXTRA_URL[] = 'flash=' . $flash;
			}
			else
			{
				$_EXTRA_URL = array('flash=' . $flash);
			}
		}
The above code assumes that the URL having no flash= parameter has the same effect as flash=0
User avatar
Brf
Support Team Member
Support Team Member
Posts: 52105
Joined: Tue May 10, 2005 7:47 pm
Location: {postrow.POSTER_FROM}
Contact:

Re: How do I pass a PHP variable (eg. $_GET)?

Post by Brf »

Uggg. T0ny beat me to it. I saw the is_array() problem too and was going to suggest that same code.

In addition, some explainations:

1. $_GET['style'] returns a string. !empty() returns a boolean. Using !empty() insures that the IF is only executed if there is actually a style parameter in the URL.

2. Only Styles Admins are allowed to change their style with a URL parameter.

3. As T0ny said, you do need the global statement, and the extra code to declare the array if it hasnt been declared already.

4. I already explained that in #1. Using request_var() to retrieve the actual value masks the parameter as an integer, avoiding hacks.

5. I do not know either. It is probably bypassing an extra IF in the current module, making sure that $_EXTRA_URL is added to the current URL without having to have another IF somewhere. You should probably do that too.
Markstar
Registered User
Posts: 72
Joined: Mon May 01, 2006 6:26 pm

Re: How do I pass a PHP variable (eg. $_GET)?

Post by Markstar »

Yes, that worked! :D

Thank you, thank you, thank you you two! :P

Now I only need to change the 'default' value to 1, which I guess is changing the code to:

Code: Select all

$flash = request_var('flash', 1);
, which I will test tonight.

Again, thanks, you guys are great! :)
Locked

Return to “[3.0.x] MOD Writers Discussion”