Solved

Global variables in HTML how to set them

Posted on 2011-09-28
7
370 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
  • 3
  • 2
  • 2
7 Comments
 
LVL 109

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 109

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
Are your AD admin tools letting you down?

Managing Active Directory can get complicated.  Often, the native tools for managing AD are just not up to the task.  The largest Active Directory installations in the world have relied on one tool to manage their day-to-day administration tasks: Hyena. Start your trial today.

 

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 109

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

What is SQL Server and how does it work?

The purpose of this paper is to provide you background on SQL Server. It’s your self-study guide for learning fundamentals. It includes both the history of SQL and its technical basics. Concepts and definitions will form the solid foundation of your future DBA expertise.

Question has a verified solution.

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

Suggested Solutions

Password hashing is better than message digests or encryption, and you should be using it instead of message digests or encryption.  Find out why and how in this article, which supplements the original article on PHP Client Registration, Login, Logo…
This article demonstrates how to create a simple responsive confirmation dialog with Ok and Cancel buttons using HTML, CSS, jQuery and Promises
The viewer will the learn the benefit of plain text editors and code an HTML5 based template for use in further tutorials.
Learn how to create flexible layouts using relative units in CSS.  New relative units added in CSS3 include vw(viewports width), vh(viewports height), vmin(minimum of viewports height and width), and vmax (maximum of viewports height and width).

777 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