# Passing Variables from snippet to snippet / Globally?

**URL:** <https://forum.getkirby.com/t/passing-variables-from-snippet-to-snippet-globally/22880>\
**Category:** Questions\
**Tags:** v3\
**Created:** [July 14, 2021, 2:22pm UTC](https://forum.getkirby.com/t/passing-variables-from-snippet-to-snippet-globally/22880 "2021-07-14T14:22:18Z")\
**Posts on this page:** 7\
**Page:** 1

<div class="post-metadata">

**Author:** ![urbanedge](https://avatars.discourse-cdn.com/v4/letter/u/cab0a1/32.png) [@urbanedge](https://forum.getkirby.com/u/urbanedge)\
**Post date:** [July 14, 2021, 2:22pm UTC](https://forum.getkirby.com/t/passing-variables-from-snippet-to-snippet-globally/22880/1 "2021-07-14T14:22:18Z")

</div>

Is there away to setup Global variables, I’ve searched and see a lot of people asking but so many different suggestions but no definitive answer?

I have a dynamic variable in the ‘_head_’ snippet which I need to pass through to the ‘_footer_’ snippet.

My page templates are build like this:

```
snippet('head');

snippet('header');

page content

snippet('footer');

```

I have a variable in the ‘_head_’ snippet:

```
$msg = "";

```

Which sometimes displays a message:

```
$msg = "This is the message";

```

In the footer snippet I need to pass in and echo that variable if it isn’t empty:

```
<?php if (!empty($msg)) { echo $msg; } ?>

```

I know I can pass it from the page the the snippet using:

```
<?php snippet('footer', ['msg' => $msg]) ?>

```

But as the variable isn’t on the page then I’m not sure how to pass the variable from snippet to snippet?

---

<div class="post-metadata">

**Author:** ![pixelijn](https://avatars.discourse-cdn.com/v4/letter/p/7ab992/32.png) [@pixelijn](https://forum.getkirby.com/u/pixelijn)\
**Post date:** [July 14, 2021, 2:33pm UTC](https://forum.getkirby.com/t/passing-variables-from-snippet-to-snippet-globally/22880/2 "2021-07-14T14:33:25Z")

</div>

What sort of content is stored in the variable, i.e. where does it come from? Do you need this variable in every template?

There are different ways to store such variable, from config options, controllers (or shared controllers) to session data. Using global variables is generally something that should be avoided.

---

<div class="post-metadata">

**Author:** ![urbanedge](https://avatars.discourse-cdn.com/v4/letter/u/cab0a1/32.png) [@urbanedge](https://forum.getkirby.com/u/urbanedge)\
**Post date:** [July 14, 2021, 2:37pm UTC](https://forum.getkirby.com/t/passing-variables-from-snippet-to-snippet-globally/22880/3 "2021-07-14T14:37:39Z")

</div>

Funny you should mention, it is actually the $msg variable from the login error on the login script you just helped me fix.

> [@Simple PHP Login not working in Kirby 3?](https://forum.getkirby.com/t/simple-php-login-not-working-in-kirby-3/22879/3):
>
> Thank you very much!

---

<div class="post-metadata">

**Author:** ![pixelijn](https://avatars.discourse-cdn.com/v4/letter/p/7ab992/32.png) [@pixelijn](https://forum.getkirby.com/u/pixelijn)\
**Post date:** [July 14, 2021, 2:53pm UTC](https://forum.getkirby.com/t/passing-variables-from-snippet-to-snippet-globally/22880/4 "2021-07-14T14:53:16Z")

</div>

Ok, so this login thingy is on every single page? But since your script creates a new session, you might as well store you message in the session in the header, then retrieve it in the footer.

---

<div class="post-metadata">

**Author:** ![urbanedge](https://avatars.discourse-cdn.com/v4/letter/u/cab0a1/32.png) [@urbanedge](https://forum.getkirby.com/u/urbanedge)\
**Post date:** [July 14, 2021, 3:05pm UTC](https://forum.getkirby.com/t/passing-variables-from-snippet-to-snippet-globally/22880/5 "2021-07-14T15:05:38Z")

</div>

The login is on every page on a popup modal, all the main navigation is shown but only some links work and some don’t, the ones that don’t launch the login modal when they are clicked (click bait).

The head has all the login php from the last post and the footer contains the modal, if the login information is wrong when the page is submitted the modal stays open and the error message appears.

**Head snippet:**

```
if(isset($_POST['username'])) {

    if($userinfo[$_POST['username']] ?? '' === md5($_POST['password'])) {
	    $_SESSION['username'] = true;
    }
    else {
	    $msg = "<h3>SORRY, YOUR USERNAME OR PASSWORD ARE INCORRECT.</h3>";
    }
}

```

**Footer snippet:**

```
<form></form>
<?php if (!empty($msg)) { echo $msg; } ?>

```

I need to pass variable through to the footer snippet.

---

<div class="post-metadata">

**Author:** ![pixelijn](https://avatars.discourse-cdn.com/v4/letter/p/7ab992/32.png) [@pixelijn](https://forum.getkirby.com/u/pixelijn)\
**Post date:** [July 14, 2021, 3:15pm UTC](https://forum.getkirby.com/t/passing-variables-from-snippet-to-snippet-globally/22880/6 "2021-07-14T15:15:30Z")

</div>

As I said, store it in the session

```auto
else {
  $_SESSION['message'] ="<h3>SORRY, YOUR USERNAME OR PASSWORD ARE INCORRECT.</h3>"; }

```

Then in the footer check if the message key is set and not empty.  
Initialize empty key in header or remove value from session on successful login.

---

<div class="post-metadata">

**Author:** ![urbanedge](https://avatars.discourse-cdn.com/v4/letter/u/cab0a1/32.png) [@urbanedge](https://forum.getkirby.com/u/urbanedge)\
**Post date:** [July 14, 2021, 3:25pm UTC](https://forum.getkirby.com/t/passing-variables-from-snippet-to-snippet-globally/22880/7 "2021-07-14T15:25:37Z")

</div>

> [@pixelijn](#):
>
> `$_SESSION['message'] ="<h3>SORRY, YOUR USERNAME OR PASSWORD ARE INCORRECT.</h3>"; }`

Thanks! Worked great.
