Jump to content

Using a cookie-stored value in a query string?


pmg206

Recommended Posts

The setup is pretty generic, but this one is kicking my butt.  Using a PHP page (Yahoo hosting, which is still on MySQL 4.1.14), the goal is to provide users a dropdown list of values that are limited to only what the page user, per their login credentials, should see.  (The project is sort of like a soccer league page, but we only want the coach to see members of his/her team in a dropdown list, even though all league players are in the single table.)

 

I figured this would be a no-brainer, based on the login ID and passing the login username/cookie into the query, but I figured wrong.  (In this way, the page would then only show them the values that were associated with their team, and pop them into a dropdown list.)

 

But how the cookie needs to be formatted is where I crumble, as it were...  The ideal sort of query, which sort of puts the web page into blank-ness, is similar to:

 

 $query_team_list = "SELECT * FROM tbl_players WHERE status = 'A' and team_id  = <?php echo $HTTP_COOKIE_VARS['cookie4team']; ?> ORDER BY fname ASC";

The team ID is pulled from the cookie created at login, i.e. 101 (always an integer, not the most elegant approach, but that's another issue).

 

If we go with this code, stuff comes back (obviously), but it's retrieving everyone.  Not stellar to scroll through dozens of names to find your folks:

 $query_team_list = "SELECT * FROM tbl_players WHERE status = 'A' and team_id  > 1 ORDER BY fname ASC";

 

Which suggests either I can't do this -- seems unlikely -- or my the means/format of inserting the cookie into the query itself is a no-no.  Thoughts?  Thanks in advance!

Link to comment
Share on other sites

$query_team_list = "SELECT * FROM tbl_players WHERE status = 'A' and team_id  = ".$HTTP_COOKIE_VARS['cookie4team']." ORDER BY fname ASC";

Should work.  I will bet what you were getting is a parsing error.  That is because you were already in php when you tried to start php again with the <?php echo $HTTP_COOKIE_VARS['cookie4team']; ?>. 

also when you are adding a variable to another variable it doe not have to be echoed.

$all_vars = $var1 . $var2 ;

Not $all_vars = echo $var1 . echo $var2;

Hope this was helpful

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.