PHP - Cookie

Networking/Security Forums -> Programming and More

Author: haydiesLocation: Hades PostPosted: Sat May 18, 2002 6:59 am    Post subject: PHP - Cookie
    ----
Any one happen to know how to use cookies in PHP?

My page writes to the cookie ok, but it can't seem to read it, the varibles are always unset....

This has been driving me nuts all night....

Author: ShaolinTigerLocation: Kuala Lumpur, Malaysia PostPosted: Sat May 18, 2002 3:40 pm    Post subject:
    ----
This is what it has in the phpBB index.php, doesn't make any sense to me but you might find it useful?

Code:


//
// Start session management
//
$userdata = session_pagestart($user_ip, PAGE_INDEX);
init_userprefs($userdata);
//
// End session management
//

$viewcat = ( !empty($HTTP_GET_VARS[POST_CAT_URL]) ) ? $HTTP_GET_VARS[POST_CAT_URL] : -1;

if( isset($HTTP_GET_VARS['mark']) || isset($HTTP_POST_VARS['mark']) )
{
   $mark_read = ( isset($HTTP_POST_VARS['mark']) ) ? $HTTP_POST_VARS['mark'] : $HTTP_GET_VARS['mark'];
}
else
{
   $mark_read = '';
}

//
// Handle marking posts
//
if( $mark_read == 'forums' )
{
   if( $userdata['session_logged_in'] )
   {
      setcookie($board_config['cookie_name'] . '_f_all', time(), 0, $board_config['cookie_path'], $board_config['cookie_domain'], $board_config['cookie_secure']);
   }

   $template->assign_vars(array(
      "META" => '<meta http-equiv="refresh" content="3;url='  .append_sid("index.$phpEx") . '">')
   );

   $message = $lang['Forums_marked_read'] . '<br /><br />' . sprintf($lang['Click_return_index'], '<a href="' . append_sid("index.$phpEx") . '">', '</a> ');

   message_die(GENERAL_MESSAGE, $message);
}
//
// End handle marking posts
//

$tracking_topics = ( isset($HTTP_COOKIE_VARS[$board_config['cookie_name'] . '_t']) ) ? unserialize($HTTP_COOKIE_VARS[$board_config['cookie_name'] . "_t"]) : array();
$tracking_forums = ( isset($HTTP_COOKIE_VARS[$board_config['cookie_name'] . '_f']) ) ? unserialize($HTTP_COOKIE_VARS[$board_config['cookie_name'] . "_f"]) : array();



Author: torrentLocation: UK PostPosted: Sat May 18, 2002 8:43 pm    Post subject:
    ----
If you are using version 4.1.x or above then grab the cookie vars from $_COOKIE['yourCookie'].

Author: pauldudeLocation: Perthshire, Scotland PostPosted: Wed May 22, 2002 11:48 am    Post subject:
    ----
There are 2 kinds of cookies, session cookies (which only apply to one instance of a browser) and expiring cookies (which will remain after the browser dies). I suspect that you are setting session cookies by using setCookie... This is a couple of lines from my Affiliate scheme redirecting:

header("Set-Cookie: affil=$aff; expires=Friday, 16-Jan-2037 00:00:00 GMT; path=/;");
header("Refresh: 0; url=$newurl");


then in future php scripts I simply use if (isset($affil)) { ...

Just make sure that you call header between <head> and </head>!!!

Paul

Author: big tomLocation: UK PostPosted: Mon Jun 10, 2002 10:44 pm    Post subject: headers
    ----
If you just send the variable you want setting as a cookie with a form to a page that sets the cookie:

Code:

<?php
setcookie("output_from_cookie", "$input_value");
header("location: next_page.php");
?>


Then when you are redirected to the next page the cookie should be able to be read as a normal variable:

Code:

<?php
echo "$output_from_cookie";
?>


Good luck.



Networking/Security Forums -> Programming and More


output generated using printer-friendly topic mod, All times are GMT + 2 Hours

Page 1 of 1

Powered by phpBB 2.0.x © 2001 phpBB Group