SitePoint Sponsor

User Tag List

Results 1 to 6 of 6
  1. #1
    SitePoint Member
    Join Date
    Sep 2000
    Location
    London, UK
    Posts
    13
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    Global Variables in PHP :confused:

    Probably a simple question...

    I want to declare a variable in PHP 4 so that it is available by all pages -- it is a name, and once the user is logged in I want the name to be available by all pages until the user logs out, without having to carry it through on the URL.

    I'm sure I could do it with sessions but I don't want to use sessions if I don't have to (of course if that is the easiest way then perhaps my arm could be twisted ).

    I've tried checking the manual but I can't find the correct way to do it.

    I'd appreciate any help or pointers anyone can give.

    Thanks.


    Jon

  2. #2
    SitePoint Member
    Join Date
    Sep 2000
    Location
    London, UK
    Posts
    13
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    Doh! Is this the answer?

    DOH!

    Is it this simple?

    PHP Code:
     <?php global $name ?>
    If so, can i still unset it the way I would unset a variable that is not declared global?

    i.e. using
    PHP Code:
    unset $name 
    Please, not too much ridicule.

    Cheers,

    Jon

  3. #3
    Mlle. Ledoyen silver trophy seanf's Avatar
    Join Date
    Jan 2001
    Location
    UK
    Posts
    7,168
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    The easiest way would be to use sessions!

    Consider your arm twisted

    Sean
    Harry Potter

    -- You lived inside my world so softly
    -- Protected only by the kindness of your nature

  4. #4
    SitePoint Addict manipura's Avatar
    Join Date
    Apr 2001
    Location
    Calgary,AB
    Posts
    345
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    If you just want to have name a name passed through all the pages then you can put a query string in your url.

    $name would be the info entered as the name. (duh?)

    <a href=page.php?name=$name>Next Page</a>

    Then whatever was in $name would be sent to the next page.

    You can also set it up so if there is no name, it won't show it in the Links URL....

    Just use an if statement, then make the query string a variable.



    if(isset($name)){
    $query_string = "?name=$name";
    }
    else{
    $query_string = "";
    }

    <a href=page.php$query_string>Next Page</a>

    That also helps for updating. Because its all set to one variable per page if you wanted to add their last name all you would have to do is change the string in that one variable and it would change it for every link on the page.

  5. #5
    SitePoint Member
    Join Date
    Sep 2000
    Location
    London, UK
    Posts
    13
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Thanks for your reply.

    I've been passing it through in the URL up until now using a similar method to the one you suggested. I don't want to have to keep coding if/else statements for the variables.

    Think I might just have to go and learn sessions!

    Nevertheless, thanks for replying.

    Cheers,


    Jon

  6. #6
    SitePoint Enthusiast
    Join Date
    Jun 2001
    Posts
    83
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    or when logging in:

    setcookie ("cookiename", "$name");

    or

    setcookie ("cookiename", "$name", time() + 3600);

    without a time used, the cookie will be deleted at the end of a browser session.

    or you can add a time, and the user has to logout.
    Basically, when you do this, the user has to be logged in, when he logs in, the cookie will be set, and without the cookie, you denie entrance.

    I don't know for sure if this cookie is limitid to only your site now, so nobody else can set it, but i know you can add it to the cookie.

    what you can do to set the cookie, is create txt files, or whatever data format you want to use (.dat .inc) and store it in a directory not accesible to outsiders, only by the script in dijksma.txt , containing the pass, lets say dijksma1,
    and then

    PHP Code:
    $name "name given by form";
    $pass "pass given by form";
    $file fopen("path/to/directory/$name.inc","r+");
    $checkpass fread($filefilesize($name.inc"));
    fclose(
    $file);
     
    if (
    $checkpass == $pass) {
    setcookie ("
    cookiename", "Dijksma"); 

    You only have to add the domain to the cookie.

    if you use this, is up to you, but it's a really simple, yet effective way to do it.


Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •