Jump to content


Photo

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


  • Please log in to reply
2 replies to this topic

#1 jdforsythe

jdforsythe
  • Members
  • PipPip
  • Member
  • 17 posts

Posted 24 August 2006 - 09:19 PM

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:

setcookie("user", "guest");
setcookie("curr_page", "home");

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

exit;

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 any 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")

#2 complex05

complex05
  • Members
  • PipPipPip
  • Advanced Member
  • 124 posts

Posted 24 August 2006 - 09:32 PM

Can you be more specific in why you are storing this "data" and what exactly it is doing in the cookies - perhaps there is an alternate method rather than using cookies.

#3 jdforsythe

jdforsythe
  • Members
  • PipPip
  • Member
  • 17 posts

Posted 24 August 2006 - 10:06 PM

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:
<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
	}
...

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

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.




0 user(s) are reading this topic

0 members, 0 guests, 0 anonymous users