Solved

Global variables in HTML how to set them

Posted on 2011-09-28
7
369 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 108

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 108

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
Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

 

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 108

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

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

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

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…
Find out what you should include to make the best professional email signature for your organization.
The viewer will learn the benefit of using external CSS files and the relationship between class and ID selectors. Create your external css file by saving it as style.css then set up your style tags: (CODE) Reference the nav tag and set your prop…
HTML5 has deprecated a few of the older ways of showing media as well as offering up a new way to create games and animations. Audio, video, and canvas are just a few of the adjustments made between XHTML and HTML5. As we learned in our last micr…

911 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

Need Help in Real-Time?

Connect with top rated Experts

21 Experts available now in Live!

Get 1:1 Help Now