Jump to content
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

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.


  • Recently Browsing   0 members

    No registered users viewing this page.

  • Similar Content

    • By iipa
      Hello forum!
      I have a site, where I want to remember two settings defined by user:
      1) Theme (light or dark)
      2) Language (Finnish or English)
      Because user makes changes to these settings on client side, I am a bit lost with how can I save them in ProcessWire $session variable? I would like to use $session for more reliable saving, and since it is only two variables I will use, I doubt it will become too resource-needy.
      I have tried using jQuery's post() -method referring to a file in Templates folder (theme.php), but I get 403 Forbidden Error. I don't like the idea of trying to post to same file user currently is in, which is why I thought having a separate file would be good in this.
      Contents of theme.php:
      <?php namespace ProcessWire; header('Access-Control-Allow-Origin: https://domain.com'); $theme = $input->post['theme']; if(!empty($theme)) $session->theme = $theme; ?> Long story short: Does anybody have any pro tips I could use with setting and getting $session variables?
    • By louisstephens
      So I have been hard at work creating url segments for a template (api) and everything is working swimmingly in creating a simple end point for svelte.js. I have however, run into a few questions that I can wrap my head around.
      In my api template I have:
      if($input->urlSegment1 === 'clients') { header('Content-Type: application/json'); $clients = $pages->find("template=clients"); $client_array = array(); foreach ($clients as $client) { $id = $client->id; $title = $client->title; $url = $client->url; $clientName = $client->client_name; $clientColor = $client->client_color->value; $assigned = $client->assigned_to->user_full_name; $client_array[] = array( 'id' => $id, 'code' => $title, 'name' => $clientName, 'associated_users' => $assigned, 'url' => $url ); } $client_json = json_encode($client_array, true); echo $client_json; } The output json from this is:
      [ { "id":1644, "code":"abc", "name":"Test Name", "associated_users":null, "url":"\/pw\/clients\/abc\/" }, { "id": 1645, "code": "xyz", "name": "Test Name", "associated_users": null, "url": "\/pw\/clients\/xyz\/" }, ] I was curious is it possible to add in "clients" before this output json so it would appear as 
      clients: [ { "id":1644, "code":"abc", "name":"Test Name", "associated_users":null, "url":"\/pw\/clients\/abc\/" }, { "id": 1645, "code": "xyz", "name": "Test Name", "associated_users": null, "url": "\/pw\/clients\/xyz\/" }, ] I was not really sure of how to tackle this in my php code, and have spent more time than I care to admit trying to figure it out. Another question I have is that "associated_users" is returning null, which in this instance is correct. It is a multi page field that is set to pull a custom name field from the users template, ie "Louis Stephens" would be associated with the first page. I understand that I need to use a foreach to get the correct data, but I was really unsure of how to place this inside an array, or update the array with the new data. Any help with any of this would greatly be appreciated.
    • By Moritz Both
      Greetings!
      For our PW project we use markup regions and, for one template, url segments. The documentation recommends throwing a new Wire404Exception() from the template when the code concludes that the url segments from the request are invalid, and so we do.
      However, the 404 page is not displayed properly. Viewing the page source in the browser we can see that the original, unmodified markup region contents from our _init.php file is prepended to the correct html output, messing the whole page up.
      Any advice is greatly appreciated.
    • 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.
×
×
  • Create New...