Solved

Global variables in HTML how to set them

Posted on 2011-09-28
7
373 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
Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 

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

On Demand Webinar - Networking for the Cloud Era

This webinar discusses:
-Common barriers companies experience when moving to the cloud
-How SD-WAN changes the way we look at networks
-Best practices customers should employ moving forward with cloud migration
-What happens behind the scenes of SteelConnect’s one-click button

Question has a verified solution.

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

Build an array called $myWeek which will hold the array elements Today, Yesterday and then builds up the rest of the week by the name of the day going back 1 week.   (CODE) (CODE) Then you just need to pass your date to the function. If i…
Not sure what the best email signature size is? Are you worried about email signature image size? Follow this best practice guide.
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…
The viewer will learn how to look for a specific file type in a local or remote server directory using PHP.

739 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