hansv

Get variable from outside Processwire

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

Share this post


Link to post
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

Share this post


Link to post
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

Share this post


Link to post
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

Share this post


Link to post
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>';
								}
	

 

  •  

Share this post


Link to post
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

Share this post


Link to post
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

  • Recently Browsing   0 members

    No registered users viewing this page.

  • Similar Content

    • By chrizz
      hey there,
      I was wondering what the best approach looks like to access the input (entity) body from a POST or PUT request. I didn't find anything in the docs if $input supports these. If anyone has dealt with this problem in the past I'd be happy to hear about your solutions
      Thanks & cheers!
    • By louisstephens
      So I have a project coming up soon and one of the goals was to use Google's AMP project for the project's mobile site. I have gone through the tutorials and think I have a good grasp on the matter, but there is still one roadblock I do not really know how to tackle. The site, which uses a responsive grid system, will look great on a mobile and desktop which was is fine by me. However, if a user comes from Google to one of our AMP pages (ie www.example.com/amp) and clicks on a url, they will then be loading our responsive mobile pages and not the amp pages. For the sake of consistency, we really want to "force" users to stay on all the amp pages.
       
      My current thoughts on how to set up this task:
      Allow url segments for all pages using "/amp" Using a simple if statement, load the amp page if it exists <?php if($input->urlSegment1) { // add "&& $input->urlSegment1 == 'amp'" if you've more urlSegments include("partials/amp-page.php"); } else { include("partials/normal.php"); } ?>  
      However, I have hit a roadblock on appending "/amp" to all pages if they came to an amp page via Google, or even if they are on mobile and visit the site. Is this even possible to do, or should we just use the amp pages (if a user comes from google) and allow them to be active on our mobile pages?  We are just trying to give the fastest load times possible, as well as give a consistent look between mobile and desktop versions. As always, I really appreciate the ideas and help.
    • By helmut2509
      Hello,
      I want to use the $session variable in functions, inside a template. According to the API this variable is available only in templates.
      But all values of the $session variable get lost, even when I use 'global $session'.
      is there any workaround for this problem?
       
    • By Pixrael
      I have a parent page (with template file) with several child pages (without template file) that are hidden and unpublished, I use them only to store information. The parent page have the url segments enabled.
      When I catch the segment in the template file, works as expected for any word, but if the url segment match the property "name" of any child page, PW launch a 404 Page Not Found.. I try all combination of "hidden" and "Publish" for the child pages.
      The idea is to render the data from the parent page plus the data of a child page, and get this data using an url segment (links generated dynamically) with the same name that this hidden page have.. I don't like having a template file for these child pages that only execute the render of the parent page with the information .. I want the previous solution if it possible
      I was able to explain well? ...What happen here?
    • By verdeandrea
      Hello,
      i read that "URL segments only apply if the requested URL did not resolve to an actual page." (from here)
      Is there a way to change this behaviour? 
      I have a parent page loading his children via ajax, and i know which children to load from url segments. So that would be nice to have url segments to have precedence instead of children's url.
      Thanks!