Jump to content

reloading page for cookie data... is there a better way?


jdforsythe

Recommended Posts

i'm working on a site and i decided to use cookies to store the username, userkey (unique auto_increment value in mysql), and current page.  the problem is the cookie data isn't available until the next page load, and on the index page i need to determine which content to display based on the cookie data.

so for the moment, in the beginning of index.php, i test to see if cookie data needs set (first page load it sets user to "guest", curr_page to "home" - or if the user is logging in, set the correct data - same with logging out, erase cookie data) and each time the cookie data is set/changed, i do something like this:

[code]
setcookie("user", "guest");
setcookie("curr_page", "home");

header("Location: /index.php");

exit;
[/code]

this works perfectly fine.  it sets the cookie data (or deletes it on logout) and then refreshes the index page so that the cookie data is available for conditional statements.

but i think there must be a better way to do this other than manually writing the header location for a redirect.  is there [i]any[/i] way to read the cookie data on the same page load where it is set?  i haven't found a way and i've been searching for hours.  the only thing i can see to do is to reload.  it isn't that big of a concern since it's such a small amount of bandwidth wasted, but using header() seems kind of like a hack.  it seems there should be a better way.  and (i hope) the site will have some heavy traffic eventually, so the minimal bandwidth multiplied by the number of page loads may actually make a difference.

any ideas?

thanks in advance.

jeremy
http://www.lepidsitedesign.com

p.s. - the site i'm working on and referring to in this post is http://www.watchmyspending.com
just in case you were wondering. (right now it's in testing, so you can log in with "test@test.com" password is "password")
Link to comment
Share on other sites

i'll elaborate a bit.

when you first load the site (index.php) it notices that no cookie is set, so you are not logged in.  it sets a cookie for user = "guest" and curr_page = "home".

if the user = "guest" then it displays a login form in the header, if the user is not "guest" then it displays a welcome message and a logout link instead of the form.

the curr_page is used for two reasons.  it helps set the class on the navigation menu so that the current page's menu item is colored differently (and also omits the link).  it also will determine which content to show in the content div.

here are some snippets of the code.

navigation:
[code]
<div id="sidebar">
  <ul> 
      <?php
        if ($_COOKIE['curr_page'] == "home") {
            ?>
            <li class="inactive">Home</li>
            <?php
}

else {
            ?>
            <li><a href="/home">Home</a></li>
            <?php
}
...
[/code]

and the content code:
[code]
<div id="content">
<?php
      $content_html_file = $_COOKIE['curr_page'] . ".html";
      include $content_html_file;
?>
</div>
[/code]

if you notice in the navigation, i link to a subfolder.  this is redirected with .htaccess / mod_rewrite back to index.php?curr_page=PAGENAME

index.php tests in the beginning whether a GET value is set for curr_page and rewrites the cookie accordingly (but only if $_COOKIE['user'] != "guest", so users who aren't logged in cannot access the various parts of the site which are intended for members).

i know i could have used a session, which i've done for other sites, but i don't have access to the php configuration on my host and it's set to not allow session id's in the url (which is good, anyway) so sessions get stored in cookies or not at all.  therefore i decided to just use cookies in the first place which expire when the browser closes.

i hope this explains it better.

thanks!

p.s. - i store the userkey (unique auto_increment unsigned int) in the cookie as well, because i use it to identify the users' data in all the other tables instead of using their username.  this saves me from querying for the userkey each time i want to look up the user's data.
Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.