Solved

Global variables in HTML how to set them

Posted on 2011-09-28
7
368 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 82

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
Better Security Awareness With Threat Intelligence

See how one of the leading financial services organizations uses Recorded Future as part of a holistic threat intelligence program to promote security awareness and proactively and efficiently identify 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 82

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

How your wiki can always stay up-to-date

Quip doubles as a “living” wiki and a project management tool that evolves with your organization. As you finish projects in Quip, the work remains, easily accessible to all team members, new and old.
- Increase transparency
- Onboard new hires faster
- Access from mobile/offline

Join & Write a Comment

Building a website can seem like a daunting task to the uninitiated but it really only requires knowledge of two basic languages: HTML and CSS.
This article discusses how to create an extensible mechanism for linked drop downs.
In this tutorial viewers will learn how to style a corner ribbon overlay for an image using CSS Create a new class by typing ".Ribbon":  Define the class' "display:" as "inline-block": Define its "position:" as "relative": Define its "overflow:" as …
The viewer will learn the basics of jQuery including how to code hide show and toggles. Reference your jQuery libraries: (CODE) Include your new external js/jQuery file: (CODE) Write your first lines of code to setup your site for jQuery…

747 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

10 Experts available now in Live!

Get 1:1 Help Now