Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Chriss Miller on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

cookies won't work!

Status
Not open for further replies.

alfalf

Programmer
Joined
Mar 6, 2003
Messages
155
Location
BA
Hello.
It seems that I cannot play around with cookies at all! Maybe I'm doing something wrong, but

here's my php.ini cookie section :

[Session]
; Handler used to store/retrieve data.
session.save_handler = files

; Argument passed to save_handler. In the case of files, this is the path
; where data files are stored. Note: Windows users have to change this
; variable in order to use PHP's session functions.
session.save_path = C:\PHP\sessiondata ; argument passed to save_handler

; Whether to use cookies.
session.use_cookies = 1


; Name of the session (used as cookie name).
session.name = PHPSESSID

; Initialize session on request startup.
session.auto_start = 0

; Lifetime in seconds of cookie or, if 0, until browser is restarted.
session.cookie_lifetime = 0

; The path for which the cookie is valid.
session.cookie_path = /

; The domain for which the cookie is valid.
session.cookie_domain = localhost

; Handler used to serialize data. php is the standard serializer of PHP.
session.serialize_handler = php

; Percentual probability that the 'garbage collection' process is started
; on every session initialization.
session.gc_probability = 1

; After this number of seconds, stored data will be seen as 'garbage' and
; cleaned up by the garbage collection process.
session.gc_maxlifetime = 1440

; Check HTTP Referer to invalidate externally stored URLs containing ids.
; HTTP_REFERER has to contain this substring for the session to be
; considered as valid.
session.referer_check =

; How many bytes to read from the file.
session.entropy_length = 0

and here's the code for using cookies:

<?php
// Check for the $LastTime cookie variable.
if ( !empty( $LastTime ) )
{
$aMessage = &quot;The last time you visited was &quot;;
$aMessage .= date( &quot;d F Y&quot;, $LastTime );
$aMessage .= &quot; at &quot;;
$aMessage .= date( &quot;h:i:s a&quot;, $LastTime );
}
else
{
$aMessage = &quot;You have not visited in the past &quot;;
$aMessage .= &quot;two weeks.&quot;;
}
// Set the $LastTime cookie that will be valid for
// two weeks
$aTwoWeeks = time() + ( 60 * 60 * 24 * 14 );
setcookie( &quot;LastTime&quot;, time(), $aTwoWeeks );
// check for the extremely important cookie array values
$aValMessage = &quot;&quot;;
if ( !empty( $CookieArray ) )
{
$aValMessage = &quot;Values: &quot; . $CookieArray[0];
$aValMessage .= &quot;, &quot; . $CookieArray[1];
$aStartValue = $CookieArray[1] + 1;
}
else
{
$aValMessage = &quot;The Values are not available!&quot;;
$aStartValue = 0;
}
// delete the extremely imporant cookie array values
setcookie( &quot;CookieArray[0]&quot; );
setcookie( &quot;CookieArray[1]&quot; );
// add the extremely imporant cookie array values
setcookie( &quot;CookieArray[0]&quot;, $aStartValue, $aTwoWeeks );
setcookie( &quot;CookieArray[1]&quot;, $aStartValue + 1, $aTwoWeeks );
?>

Needles to say that cookies work just fine but if written here in javascript.

I use: W2K, Apache2, PHP4 (cgi-binary), MySQL (if neccesary).

Where am I wrong here? Help!
 
You maybe have Register-globals off in php.ini. I would try it this way:
Code:
<?php
    // Check for the $LastTime cookie variable.
    if(isset($_COOKIE['LastTime'])) {
        $LastTime=$_COOKIE['LastTime'];
        $aMessage  = &quot;The last time you visited was &quot;;
        $aMessage .= date( &quot;d F Y&quot;, $LastTime );
        $aMessage .= &quot; at &quot;;
        $aMessage .= date( &quot;h:i:s a&quot;, $LastTime );
    }
// and so on
I do the work with cookies this way and it works. Also make sure you have cookies enabled in your browser ;-)
 
I agree with gizmicek. It is probably a register-globals issue.
Write a one liner with
Code:
<?php
phpinfo();
?>
and inspect the settings for register globals. If it is PHP 4.2.1 and later it will be by default off - and that is good because it closes some security holes and avenues to introduce arbitrary values into scripts.
It is advisable to use the superglobal arrays $_POST, $_GET, $_COOKIE, $_SESSION etc. instead. These arrays are superglobal, so available throughout the scope of the entire script.
 
OK. I did try it out but still have problems, most probablly I'm missing something trivial.
I spoted in my PHP.ini this:

REQUEST_METHOD | GET

Shouldn't it be POST?


Cookies are enabled in browser (mozila 4.0 compatible=MSIE 5.01). STILL PROBLEMS!

If you have an example for me to try out even if I tried yours?

Thx.
 
Why? Are you posting a POST-method form to the phpinfo() script?

$_SERVER['REQUEST_METHOD'] will be &quot;GET&quot; unless a POST-method form is submitted.

If you want to test whether cookies are working, use the simplest possible example.

Create two scripts:
Code:
<?php
setcookie ('FOO', 'BAR', time() + 3600);
print &quot;done.&quot;;
?>

and

Code:
<?php
print '<pre>';
print_r ($_COOKIE);
?>


Point your browser to the first script, then to the second. What is the output of the second script?

If it doesn't work with a particular browser, modify the set_cookie() invocation to set a cookie path of '/'. (As I recall, Netscape was always picky about cookie paths.)

Want the best answers? Ask the best questions: TANSTAAFL!!
 
Thanks very much!

The thing WAS TRIVIAL.

I havent noticed that cookie file: administrator@localhost,IS APPENDED!

Why did I think my localhost s. will make different cookie files (perhaps sessions made me think that way)? :-) Silly me!

Thx!
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top