Jump to content

Get variable from outside Processwire


hansv
 Share

Recommended Posts

Hi everybody

I want to catch a variable from outside processwire into _main.php

 if(isset($_GET['u']) && $_GET['u'] !== ''){
 	$gebruikersnaam = $_GET['u'];
...
...

This is working fine in a php file in a non processwire environment. 

In a processwire environment, I get the variable when I refer to /site/templates/_main.php but a great part of my template-code is not shown. 

From outside processwire I refer to    mydomain/index.php.  If I place   $gebruikersnaam = $_GET['u'];  in index.php, how can I pass through my variable to _main.php?   Or is there an other solution?

thx

hansv

Link to comment
Share on other sites

To be honest I'm a bit confused by your question (for an example I have no idea what you mean by "referring to mydomain/index.php" and how this relates to ProcessWire), but if you're trying to read a GET variable (mydomain/?u=value) in one of your template files then you can access it via $input->get->u.

You can use $_GET['u'] as well since $input is mainly just a wrapper over GET / POST / COOKIE, but I'd recommend using $input in the context of ProcessWire 🙂

  • Like 2
Link to comment
Share on other sites

You can add get params to every url:

example.com/apagename/?param=value

To work with it in processwire template files, we have the $input variable instead of the global $_GET variable. There is also $sanitizer. 

I'm on mobile, so please look out in the docs for the $input variable. (poviding links via mobile is a pain 😀)

  • Like 3
Link to comment
Share on other sites

@teppo@horst

thx for your quick, very usefull, respons

The combination of $input->get and allow url-segments in the home page did the trick. 

I was to much focused on the _main.php, but the real page was home.php.  With the url-segments allowed in the home template, the magic of processwire was there.  

  if(($input->get->u) !== ''){
        $gebruikersnaam = $input->get->text('u');	

 

  • Like 3
Link to comment
Share on other sites

The solution is working fine.  The code is placed in _main.php and is shown in the header-part of the template.

When going to another page, the information is lost and I get the error:  

 Trying to get property 'naam' of non-object in /....../_main.php on line 83.

I tried session-variables.  The variables are stored, but when I go to another page the sesson-variables are not retrieved.

My code

				$gebruikerarray = json_decode($gebruiker);	
								    $session->naam = $gebruikerarray->naam;	
								    $session->voornaam = $gebruikerarray->voornaam;	
								    $session->klas = $gebruikerarray->groups[0]->name;	
								    $session->email = $gebruikerarray->emailadres;	
								    $session->email_coaccount1 = $gebruikerarray->email_coaccount1; 
								    $session->email_coaccount2 = $gebruikerarray->email_coaccount2;	
								  
								    							   

	     						echo 'Bestelling voor ' . $session->naam  . ' ' . $session->voornaam . ' -  ' . $session->klas . '<br>';  
	  							echo 'Email:  ' . $session->email  . '<br>';
	  							if ($session->email_coaccount2 !== '' ) {
									echo 'Email co-account 1: ' . $session->email_coaccount1  . '<br>';
								}
	  							if ($session->email_coaccount2 !== '' ) {
									echo 'Email co-account 2: ' . $session->email_coaccount2 . '<br>';
								}
	

 

  •  
Link to comment
Share on other sites

You overide each pagereload the complete session vars.

I think that the $gebruiker is not created and available on every page load, but only on a special template.

You may use a conditional that secures you that a (new) gebruiker is sent and need to be transfered into the session:

$gebruikerarray = json_decode($gebruiker);
if(!empty($gebruikerarray->naam)) {
    // add new data to sessions vars
    ...
}

OR you look if $gebruiker is available or not, or if a post submitt was sent, or ...

 

  • Like 2
Link to comment
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
 Share

×
×
  • Create New...