Hey

I want to declare a variable that is global that I can use (and modify) all over my website. Ive read that I may be able to do it with jQuery but not sure how to (more importantly, where).

Its a simple text field, nothing else. Specifically it must be able to be accessed in Javascript, PHP and phtml (which is PHP allowed with HTML right?)

Thank you!

Dani AI

Generated

This thread is mixing two different problems: a runtime "global" available in JavaScript on every page, and a persistent value that PHP can read when pages are rendered. Several good suggestions already appear — for server sessions, for client storage options, for a shared include, and for cookie helpers — so the short, practical patterns below clarify which to use and why.

When PHP must see the value on the next page load, set a cookie from JavaScript (or set it server-side). A cookie written with path=/ is sent to every page on the site and can be read by PHP via $_COOKIE. Example (vanilla JS + PHP):

// set cookie so PHP receives it on the next request
var value = encodeURIComponent('some text value');
document.cookie = 'siteVar=' + value + '; path=/; max-age=' + (60*60*24*7);
// in PHTML/PHP
if (isset($_COOKIE['siteVar'])) {
    $siteVar = urldecode($_COOKIE['siteVar']);
}

If the value only needs to be available to JavaScript across pages, localStorage is simpler and larger than cookies, but it is NOT sent to the server automatically. To make a client-only value available to PHP, post it to the server (AJAX or form) so PHP can store it in $_SESSION or a database. Quick localStorage usage:

localStorage.setItem('siteVar', 'some text');
var v = localStorage.getItem('siteVar');

A shared JS file (the include pattern mentioned by ) is useful to declare a small namespace and avoid accidental overwrites:

var Site = window.Site || {};
Site.sharedVar = Site.sharedVar || null;

Notes and cautions: JavaScript cannot create HttpOnly cookies (those must be set by the server); use HttpOnly + Secure + SameSite for session identifiers. Avoid putting secrets client-side (cookies/localStorage are attack vectors for XSS). Cookies are limited to ~4KB each; localStorage has larger quota. PHTML is just a PHP-containing template file — reading $_COOKIE or echoing PHP into inline JS works the same as in .php files.

Recommended Answers

All 8 Replies

You could try making a script and insert it on all your pages. In php, you would have to write the session on top of every page, so why not include a script on every page.

<script type="text/javascript">
$(document).ready(function(){
  $("a").click(function(){
    // your global variable
    var z = "cheese";
    //gets the href attribute
    var x = $(this).attr("href");
    //go to links location with variable
    location.href=x+z;
  });
});
</script>
<body>
<a href="www.google.com/#q=">click me</a>
</body

Hope this helps

how about using $_SESSION ?? http://w3schools.com/php/php_sessions.asp

But in a Javascript context (well or HTML, depends) I cant store a PHP session variable can I?

You could try making a script and insert it on all your pages. In php, you would have to write the session on top of every page, so why not include a script on every page.

Hope this helps

I already have a Javascript which I execute when I click the button that interacts (so to speak) with the name. I tired to use "document.cookie" but obviously it only applies to the CURRENT page, not any others.

Also, if I do indeed load a script on every page, besides the penalty in loading it, I would have to make sure that I dont overwrite the variable each time I load it right? I just use it on ONE page (and it is opinional)

Member Avatar for Member #905211

Cookies, local storage, indexDB, WebSQL, you can use any of these to keep persistent data accross your site.

I'd suggest cookies or local storage, you will have browser issues with indexDB and WebSQL and as long as it's not a lot of data.

Cookies, local storage, indexDB, WebSQL, you can use any of these to keep persistent data accross your site. I'd suggest cookies or local storage, you will have browser issues with indexDB and WebSQL and as long as it's not a lot of data.

Not really sure whats the difference between Cookies and local storage :P but I dont mind either way.

My main object right now is to

1: Clicking a button that executes Javascript, inside a Javascript function, store a value in a Cookie (or local store)
2: In a completely different PHTML page, have it show that variable's value.

Thank you for helping :)

use jquery cookie plugin, it's easy to set and retrieve a cookie. https://github.com/carhartl/jquery-cookie

it's almost 2kb only.

Set an expiring cookie:

$.cookie('the_cookie', 'the_value', { expires: 7 });

Set a session cookie:

$.cookie('the_cookie', 'the_value');

Retrieve that cookie:

$.cookie('the_cookie');

Delete that cookie:

$.cookie('the_cookie', null);

So simple yet effective. You can store any values you want to that cookie, you could reset it's value too.

use jquery cookie plugin, it's easy to set and retrieve a cookie. https://github.com/carhartl/jquery-cookie it's almost 2kb only. Set an expiring cookie: $.cookie('the_cookie', 'the_value', { expires: 7 }); Set a session cookie: $.cookie('the_cookie', 'the_value'); Retrieve that cookie: $.cookie('the_cookie'); Delete that cookie: $.cookie('the_cookie', null); So simple yet effective. You can store any values you want to that cookie, you could reset it's value too.

(Javascript is not a strong point of mine)

1: How do I use this in a PHTML page? I cant seem to include this library
2: How do I include this script in another JS file? Thus making "$.cookie" avaliable

try using jquery.jstore

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.