vBulletin and forms: parameters via post or get

Imagine you’ve got a vbulletin forum running in your site. Lets suppose you want to add a new form to your forum.

How does vbulletin deal with parameters? You can see the official documentation here. I prefer to explain things with an example, so lets go for it.

I will take for granted that you know how to make an html form. If you don’t… start with the basics.

The new_form.php

Create a new file, paste the code below and name it new_form.php.

<?php
error_reporting(E_ALL & ~E_NOTICE);
define('THIS_SCRIPT', 'test');  
require_once('./global.php'); /* load vBulletin functions, MySQL connections, blablabla */
 
// The following two lines will read GET parameters (as stated by the 'g') and check they are strings.
// It is not recommended to use $_GET['firstparameter'] since vBulletin has its own functions to retrieve parameter values
$firstparameter = $vbulletin->input->clean_gpc('g', 'firstparameter ', TYPE_STR);
$anotherparameter = $vbulletin->input->clean_gpc('g', 'anotherparameter ', TYPE_STR);
 
// When doing a clean_gpc the $vbulletin->GPC array is filled with the parameter values.
 
// Now lets print those parameter values...
// You could use print_r($vbulletin->GPC) instead, but I kind of prefer this way
$params = $vbulletin->GPC;
$my_str = '';
    foreach ($params as $key => $value) {
        $my_str = $my_str."Key: $key; Value: $value \n";
    }
?>

Now if you run that php in your webserver (like http://yourservername.com/new_form.php?firstparameter=Hello&anotherparameter=World) a new output will be produced.

Key: firstparameter; Value: Hello
Key: anotherparameter; Value: World

How do I check $_REQUEST or $_POST parameters?

Just a small change! If your form is done with method="post" you should only change:

$firstparameter = $vbulletin->input->clean_gpc('g', 'firstparameter ', TYPE_STR);

to

$firstparameter = $vbulletin->input->clean_gpc('p', 'firstparameter ', TYPE_STR);

You could only code this new_form.php in a way it is able to read both GET and POST parameters, just using REQUEST:

$firstparameter = $vbulletin->input->clean_gpc('r', 'firstparameter ', TYPE_STR);

Can I check a complete array of parameters in just one command?

Of course you can! In the following example I’ve assigned the output of clean_array_gpc to an array, but there is no need to do this since it is already stored into $vbulletin->GPC

$my_array = $vbulletin->input->clean_array_gpc('r', array(
	'firstparameter'	=>	TYPE_BOOL,
	'anotherparameter' 	=>	TYPE_BOOL
));

Which are the valid data types?

Valid data types are:

    * TYPE_BOOL - Boolean
    * TYPE_INT - Integer
    * TYPE_UINT - Unsigned Integer
    * TYPE_NUM - Floating Point Number
    * TYPE_UNUM - Unsigned Floating Point Number
    * TYPE_UNIXTIME - Unix Timestamp (Unsigned Integer)
    * TYPE_STR - Trimmed String (No leading or trailing whitespace)
    * TYPE_NOTRIM - String
    * TYPE_NOHTML - Trimmed String sent through htmlspecialchars_uni()
    * TYPE_ARRAY - Array
    * TYPE_FILE - File
    * TYPE_NOCLEAN - Unvalidated

Well done, VB guys :)

*** spanish readers can refer to this vbhispano tutorial.

Related posts:

  1. vBulletin: change the redirect or the url in the login process
  2. vBulletin 4: Add right or left sidebar [SOLVED]
  3. vBulletin 4: Disable plugin/mod per style [SOLVED]
  4. vBulletin 4.1.7 – Instalar spanish language pack [RESUELTO]
  5. vBulletin: change sticky text style [SOLVED]

Leave a Reply

Paypal donate

Please help me keep this blog up by donating.

Por favor, ayúdame a continuar con el blog donando.