PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!
Moderator: General Moderators
nick2
Forum Contributor
Posts: 118 Joined: Fri Jul 25, 2003 2:34 pm
Post
by nick2 » Fri Aug 01, 2003 7:00 pm
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']);
#
?>
nielsene
DevNet Resident
Posts: 1834 Joined: Fri Aug 16, 2002 8:57 am
Location: Watertown, MA
Post
by nielsene » Fri Aug 01, 2003 7:25 pm
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.
nick2
Forum Contributor
Posts: 118 Joined: Fri Jul 25, 2003 2:34 pm
Post
by nick2 » Fri Aug 01, 2003 7:41 pm
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;
}
?>
?>
?>
nielsene
DevNet Resident
Posts: 1834 Joined: Fri Aug 16, 2002 8:57 am
Location: Watertown, MA
Post
by nielsene » Fri Aug 01, 2003 7:45 pm
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.....