Page 1 of 1

cookies error.

Posted: Fri Aug 01, 2003 7:00 pm
by nick2
Been trying to figure out what I did wrong here on line 2.. maybe you could tell me:

Code: Select all

<?php
<?
if ($_COOKIE[auth[0]]== "ok") {
} else {
header( "Location: Http://slices.net/tradingsystem/nologin.html");
exit;
}
?>
?>
I made the cookies an array.. heres that code.:

Code: Select all

<?php
#cookies#
if ($num != 0) {
$cookie_name = "auth";
$cookie_value = "ok";
$cookie_expire = "0";
$cookie_domain = "www.slices.net";
setcookie($cookie_name[0], $cookie_value, $cookie_expire, "/" ,
$cookie_domain, 0);
setcookie($cookie_name[1], $_POST['ID']);
#
?>

Posted: Fri Aug 01, 2003 7:25 pm
by nielsene

Code: Select all

<?php
error_reporting(E_ALL);
if (isset($_COOKIE["auth"]) && $_COOKIE["auth"][0]== "ok") {
  $msg = "Cookie Set";
} else {
  $msg = "Cookie Not Set";
}
$cookie_name = "auth";
$cookie_value = "ok";
$cookie_expire = "0";
setcookie("{$cookie_name}[0]", $cookie_value, $cookie_expire);
setcookie("{$cookie_name}[1]", "second", $cookie_expire);
echo $msg;

?>
On the first load is says "Cookie Not Set"; on a reload "Cookie Set"

Your problem was the name of the cookie. $cookie_name[0] as you wrote it is undefined. What you wanted was a cookie named the value in $cookie_name, then turned into an array. The braces I added around $cookie_name make PHP consider the [0] as NOT part of the variable to interpolate. And the entire thing has to be a string, hence the quotes.

Posted: Fri Aug 01, 2003 7:41 pm
by nick2
doesn't work.

login > auth_server.php

Code: Select all

<?php
$db_name = "slices_net";
$table_name = "trading";
#
$connection = mysql_connect("localhost", "slices", "thibault1")
or die(mysql_error());
#
$db = mysql_select_db($db_name, $connection) or die(mysql_error());
#
$sql = "SELECT * FROM $table_name WHERE ID = '$_POST[ID]' AND
password = password('$_POST[password]')";
#
$result = mysql_query($sql,$connection) or die(mysql_error());
#
$num = mysql_num_rows($result);
#cookies#
if ($num != 0) {
$cookie_name = "auth";
$cookie_value = "ok";
$cookie_expire = "0";
$cookie_domain = "www.slices.net";
setcookie("{$cookie_name[0]}", $cookie_value, $cookie_expire, "/" ,
$cookie_domain, 0);
setcookie("{$cookie_name[1]}", $_POST['ID']);
#
echo "<P>You were logged in successfuly</P>";
echo "<a href=index.php>Home</a>";
} else {
header("location: Http://slices.net/tradingsystem/login.php");
exit;
}
?>
?>
Protect pages:

Code: Select all

<?php
<?
if (isset($_COOKIE["auth"]) && $_COOKIE["auth"][0]== "ok") {
} else {
header( "Location: Http://slices.net/tradingsystem/nologin.html");
exit;
}
?>

?>
?>

Posted: Fri Aug 01, 2003 7:45 pm
by nielsene
Look at my code, look at your code.

Notice where I put the braces {} relative to the variable name and the brackets []. Notice where you put them.....