Solved

Global variables in HTML how to set them

Posted on 2011-09-28
7
374 Views
Last Modified: 2012-05-12
I want to be able to refer to global variables, throughout my webpages.

I have set some variables and I can't read read them even in the page I set them, see the code, so I believe that I have not set the globals correctly if someone could check for me please.


When I searched EE for a solution there was lots of comments about the badness of global variables.

I also tried setting $_POST variables and that wasn't successful either.

Can someone help

Thank you for reading this

John
$role=$acct->getUserRole($_POST['fusername']);
        $user_ID=$acct->getUser_ID('fusername');
        $GLOBALS['user_ID_loggedin']=$user_ID;
        $GLOBALS['user_name_logged']=$_POST['fusername'];
        $GLOBALS['user_role']=$role;
        foreach ($GLOBALS as $key => $value)
        {
            echo htmlspecialchars('$key : $value');
            echo "<br>";
        }
        //header("Location: SecretPage.php");
        //exit();

Open in new window

image.docx
0
Comment
Question by:johnecollins
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 3
  • 2
  • 2
7 Comments
 
LVL 110

Accepted Solution

by:
Ray Paseur earned 334 total points
ID: 36718735
Buy this book.   It will get you a good foundation in how PHP works.
http://www.sitepoint.com/books/phpmysql4/

My guess is that you want to use the PHP session.  It lets you carry variables from one page to the next.
http://php.net/manual/en/function.session-start.php
0
 
LVL 110

Assisted Solution

by:Ray Paseur
Ray Paseur earned 334 total points
ID: 36718750
From the variable names posted here it looks like you may be trying to do client authentication in PHP.  If so, this article will show you the design pattern.
http://www.experts-exchange.com/Web_Development/Web_Languages-Standards/PHP/A_2391-PHP-login-logout-and-easy-access-control.html
0
 
LVL 83

Assisted Solution

by:Dave Baldwin
Dave Baldwin earned 166 total points
ID: 36718859
There are two primary methods of storage (that Ray referred to above) available to you.  The first is $_SESSION variables which apply to a particular session and user.  They expire after a short time of inactivity.  The second is databases like MySQL which don't expire.  You would have to include some id info to find data for a particular user.

All other data and variables disappear when the PHP script finishes.  The only thing left is what you sent to the browser.
0
Don't Cry: How Liquid Web is Ensuring Security

WannaCry is just the start. Read how Liquid Web is protecting itself and its customers against new threats.

 

Author Comment

by:johnecollins
ID: 36718925
Hi Ray,

Thanks for the swift answers, I meant to contact you because I already have that bokk and have read most of it. I will consult it again and and the other two sites. As I was struggling with the things I was doing I enrolled on two courses on the sitepoint site the getting started with CSS and javascript programming for the web. I am feeling the benefit for them and it's solved a few problems for me and I'll be charging upwards and onwards ;-)

If I ever make it to America I'd be very pleased to come and shake your hand and thank you for all the help you have given me.

Cheers for now

John
0
 

Author Closing Comment

by:johnecollins
ID: 36718951
Thank you both for your help and the quick response

John
0
 
LVL 83

Expert Comment

by:Dave Baldwin
ID: 36718973
You're welcome, glad to help.
0
 
LVL 110

Expert Comment

by:Ray Paseur
ID: 36718996
Thanks for your kind words, John.  In addition to what DaveBaldwin said, there is another way to pass information from one request to the next (in HTTP parlance, a page load is a "request") and that is to use cookies.  In fact the session handler uses cookies to carry a pointer.  The pointer lets each script find the values in the session array.

It works something like this:

session_start();
$_SESSION["dogName"] = "Fido";

Now the value in $_SESSION["dogName"] will be available to other scripts.

Couple of notes.  Session_start() must be called first before there is any browser output at all, even invisible whitespace.  A good way to ensure that catastrophe is not left to chance is to put the session_start() into some kind of conditional structure, like an if() statement.  A good way to reduce the risk of error is to make session_start() the first line in your php script.  The $_SESSION array is undefined but mutable even if you do not execute session_start(); so be aware!
0

Featured Post

Secure Your WordPress Site: 5 Essential Approaches

WordPress is the web's most popular CMS, but its dominance also makes it a target for attackers. Our eBook will show you how to:

Prevent costly exploits of core and plugin vulnerabilities
Repel automated attacks
Lock down your dashboard, secure your code, and protect your users

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

When it comes to write a Context Sensitive Help (an online help that is obtained from a specific point in state of software to provide help with that state) ,  first we need to make the file that contains all topics, which are given exclusive IDs. …
Finding original email is quite difficult due to their duplicates. From this article, you will come to know why multiple duplicates of same emails appear and how to delete duplicate emails from Outlook securely and instantly while vital emails remai…
In this tutorial viewers will learn how to code links for mobile sites that, once clicked, send a call or text to a specified number. For a telephone link (once clicked, calls a number), begin with a normal "<a href=" link tag. For the href, specify…
In this tutorial viewers will learn how to embed an audio file in a webpage using HTML5. Ensure your DOCTYPE declaration is set to HTML5: : The declaration should display (CODE) HTML5 is supported by the most recent versions of all major browsers…

615 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question