Page 1 of 1

cookie problem

Posted: Sun Sep 25, 2005 2:39 pm
by sheepz
Just barely learning about how to use cookies. I have "cookie_test.php" setting the cookies and trying to pass it on to a link within "cookie_test.php" named "cookie1.php" when trying to echo the results on "cookie1.php" i receive nothing.

Here is "cookie_test.php"

Code: Select all

<?
$cookie_name = "test";
$cookie_value = "string";
$cookie_expire = "0";
$cookie_domain = "127.0.0.1";

setcookie($cookie_name, $cookie_value, $cookie_expire, "/" , $cookie_domain, 0);
echo "$cookie_name, $cookie_value</p>";
?>

<html>
<head>
<title> testing </title>
</head>
<body>

<a href=cookie1.php> cookie1 </a>

</body></html>
results for "cookie_test.php"

Code: Select all

test, string

cookie1
here is "cookie1.php"

Code: Select all

<?
echo "$cookie_name, $cookie_value";
?>
results for "cookie1.php"

Code: Select all

Notice: Undefined variable: cookie_name in c:\Inetpub\wwwroot\cookie1.php on line 2

Notice: Undefined variable: cookie_value in c:\Inetpub\wwwroot\cookie1.php on line 2
,
I'm not exactly sure why the cookies are not passing thru to the link? Is there something I need to edit within php.ini? Or something wrong with my PHP5 and IIS setup? I'm sure it's just me. thanks for help!

Posted: Sun Sep 25, 2005 3:17 pm
by shiznatix
gotta use the global $_COOKIE

Code: Select all

echo $_COOKIE['test'];

Posted: Sun Sep 25, 2005 3:19 pm
by feyd
the cookie, if properly created, will be stored in $_COOKIE, it will not be created as a variable by itself unless you have register_globals on, which you should not. They will not have the variable names you used (unless you actually passed the same thing as a string in the variable)

Posted: Sun Sep 25, 2005 3:26 pm
by sheepz
I changed the "cookie1.php" code to

Code: Select all

<?
echo $_COOKIE['test'];
?>
unfortunately I receive:

Code: Select all

Notice: Undefined index: test in c:\Inetpub\wwwroot\cookie1.php on line 2
How would I check if the cookie passed thru to the link? I tried echoing again but getting a notice only.

Posted: Sun Sep 25, 2005 3:30 pm
by shiznatix
since its on the same domain it should be passed through no problem. do you have cookies enabled on your browser? since you set the expire time to be 0 it will be erased after you close your browser, like a session.

if none of that is working try setting the cookie with just 2 paramaters (name and value) or with three (name, value, and a bigger time, like a 1 week expiration)

check the manual for more details

edit: also try print_r($_COOKIES); and see what you get

Posted: Sun Sep 25, 2005 3:40 pm
by sheepz
I have my browser on "accept all cookies"

changed cookie_test.php to:

Code: Select all

<?
$cookie_name = "test";
$cookie_value = "string";
$cookie_expire = "3600";

setcookie($cookie_name, $cookie_value, $cookie_expire, "/");
?>

<html>
<head>
<title> testing </title>
</head>
<body>

<a href=cookie1.php> cookie1 </a>

</body></html>
changed cookie1.php to:

Code: Select all

<?
print_r($_COOKIES);
?>
results

Code: Select all

Notice: Undefined variable: _COOKIES in c:\Inetpub\wwwroot\cookie1.php on line 2
don't really understand the errors, going to take a look at the manual

Posted: Sun Sep 25, 2005 3:45 pm
by feyd
$_COOKIE

it's not plural :P

Posted: Sun Sep 25, 2005 4:12 pm
by sheepz
feyd, and shiz thanks for all your help i changed my "setcookie()" to just the name and value and i get a successful cookie respone.

cookie_test.php

Code: Select all

<?

$cookie_name = "test";
$cookie_value = "string";

setcookie($cookie_name, $cookie_value);
?>

<html>
<head>
<title> testing </title>
</head>
<body>

<a href=cookie1.php> cookie1 </a>

</body></html>
cookie1.php

Code: Select all

<?
print_r($_COOKIE);

IF ($_COOKIE[test] == "string")
    {
      echo "good cookie";
    }
    ELSE
    {
      echo "bad cookie";
    }
?>
result

Code: Select all

Array ( [test] => string ) 
Notice: Use of undefined constant test - assumed 'test' in c:\Inetpub\wwwroot\cookie1.php on line 4
good cookie
thank you both. I'm just learning to make a simple login page. I'm not sure how secure it will be by just using cookies as a check point. Should I give a sha1 hash number to be checked as well with the cookie?

Posted: Sun Sep 25, 2005 4:15 pm
by feyd
using sessions is typically more secure, but not by a whole lot (depending on what data is being stored and how much you trust it.)

As for your notice.. you need to quote all named array indices:

Code: Select all

if( $_COOKIE['test'] == 'string')

same question, but entered more code

Posted: Sun Sep 25, 2005 7:46 pm
by sheepz
I have 2 files "sessionTEST.php" and "session1.php" I can see the cookies being set and displayed, thx feyd and shiz correcting my code. Here's what I have so far

sessionTEST.php

Code: Select all

<?

// define VARs
$user=$_POST['username'];
$pass=$_POST['password'];

// IF statement to check STATIC username and password
IF (($user == "adam") AND ($pass == "password"))
    {
      // setcookie name = test and value = $user
      setcookie("test", $user);
      
      // display block
      $display_block = "
	  <h1>my Menu</h1>
      <p><strong>My Music</strong></p>

      <!-- Admin list of php scripts to run and order the sql query -->
      <ul>
      <li><a href=session1.php> session1 </a>
      <li><a href=session2.php> session2 </a>
      <li><a href=directions.html> direction </a>
      <li><a href=sel_byartist.php> Order by artist </a>
      </ul>";
    }
    ELSE
    {
    // Login failure page
	$display_block = "Login Faild. <br>
	
	<FORM METHOD=post ACTION=sessiontest.php>
	
    <!-- create artist fname box -->
    <TABLE BORDER=2 BORDERCOLOR=#336699 CELLPADDING=2 CELLSPACING=1 WIDTH=>
    <tr>
    <td valign=top>
    <p><strong>Login name:</strong><br>
    <INPUT TYPE=text NAME=username SIZE=20 maxlength=20></p>
    </td>

    <!-- create artist lname box -->
    <td valign=bottom colspan=2>
    <p><strong>Passw0rd:</strong><br>
    <INPUT TYPE=password NAME=password SIZE=20 maxlength=20></p>
    </td>
    </tr>


    <!-- Notes text field area and submit button -->
    <tr>
    <td valign=top colspan=2 align=center>
    <p><strong>Adams Notes:</strong><br>
    <textarea name=my_notes cols=35 rows=5 wrap=virtual></textarea></p>
    <INPUT TYPE=submit NAME=submit value=login></p></form>
    </td>
    </tr>
    </table>";
   }

?>

<html>
<head>
<title>Session test</title>
</head>
<body>

<?
echo "$display_block";
// display cookie for testing
print_r($_COOKIE);
?>

</body></html>
result

Code: Select all

my Menu
My Music

session1 
session2 
direction 
Order by artist 
Array ( [test] => adam )
here is the connecting page link "session1.php"

Code: Select all

<?
// define VAR
$user = $_POST['username'];
$pass = $_POST['password'];

// IF statement to check for success
IF ($_COOKIE['test'] == "$user")
    {
      echo "good cookie";
    }
    ELSE
    {
      echo "bad cookie</p>";
    }
    
print_r($_COOKIE);
?>
result

Code: Select all

Notice: Undefined index: username in c:\Inetpub\wwwroot\session1.php on line 3

Notice: Undefined index: password in c:\Inetpub\wwwroot\session1.php on line 4
bad cookie

Array ( [test] => adam )
I see from the array at the end, the name of the cookie is "test" and the $user variable is also known which is "adam" I'm not exactly sure why would it give me a FALSE on the IF statement? And why does it say undefined index. I thought I already defined it in the other file "sessionTEST.php"

Posted: Sun Sep 25, 2005 7:57 pm
by feyd
post data will not be available on any further pages after submission unless resubmitted to said pages. Therefore, you must pass the post data from the first script to the second in some fashion.

Posted: Sun Sep 25, 2005 7:59 pm
by sheepz
hrm... I was afraid of that. How do I go about "passing" the info to other pages within the site?

Posted: Sun Sep 25, 2005 8:07 pm
by feyd
another form post, url (get) variables, sessions ;)