[RESOLVED] Setting cookies
Moderator: General Moderators
- Ambush Commander
- DevNet Master
- Posts: 3698
- Joined: Mon Oct 25, 2004 9:29 pm
- Location: New Jersey, US
- RobertGonzalez
- Site Administrator
- Posts: 14293
- Joined: Tue Sep 09, 2003 6:04 pm
- Location: Fremont, CA, USA
- RobertGonzalez
- Site Administrator
- Posts: 14293
- Joined: Tue Sep 09, 2003 6:04 pm
- Location: Fremont, CA, USA
I'm not too sure if it is setting it correctly though. My administration page is setup to read the cookie with the following code, and it's keeps telling me that I'm not authorized. So that leads me to believe it's not setting properly.
Code: Select all
if (isset($_COOKIE['user']))
{
$cookie_info = explode("-", $_COOKIE['user']);
$name = $cookie_info[0];
$pass = $cookie_info[1];
$dbh = mysql_connect ("localhost", "name", "password") or die ('Database Connection Error: ' . mysql_error());
mysql_select_db ("database");
$query = "SELECT * FROM Login WHERE name='$name' AND password='$pass'";
$sql = mysql_query($query); //or die(mysql_error());
$count = mysql_num_rows($sql);
$queryadmin = "SELECT * FROM Login WHERE name='$name' AND admin='-1'";
$sqladmin = mysql_query($queryadmin);
$countadmin = mysql_num_rows($sqladmin);
if ($countadmin == 1)
{
echo "<center><span class='RED'><h1>Administration Panel</h1></span>";
echo "<BR><a href='../MembersOnly.php'>Members Only</a>";
echo "<BR><a href='../Logout.php'>Logout</a></center>";
}
}
else
{
echo "<CENTER><SPAN CLASS='RED'>You are not an administrator</SPAN>";
echo "<BR><A HREF='../index.php'>Home</a></CENTER>";
}- Ambush Commander
- DevNet Master
- Posts: 3698
- Joined: Mon Oct 25, 2004 9:29 pm
- Location: New Jersey, US
- Ambush Commander
- DevNet Master
- Posts: 3698
- Joined: Mon Oct 25, 2004 9:29 pm
- Location: New Jersey, US
- Ambush Commander
- DevNet Master
- Posts: 3698
- Joined: Mon Oct 25, 2004 9:29 pm
- Location: New Jersey, US
OK, cleared the cookies and got it work. I also had to change one part in my body code. It was trying to act like it was posted to if the cookie was set, but I switched the following code:
To:
So it authenticates you properly if you go to the main page and the cookie is still set. But besides that, it looks likes it's functioning corrctly at this point, I got to tinker with the logout page so it clears it properly, but step 1 is complete. Thanks.
Code: Select all
$name = $_POST['name'];
$pass = md5($_POST['pass']);Code: Select all
$cookie_info = explode("-", $_COOKIE['user']);
$name = $cookie_info[0];
$pass = $cookie_info[1];I don't know what I'm doing wrong, but I thought clearing cookies was easier. This is my code at the top:
Code: Select all
if (isset($_COOKIE['user']))
{
setcookie ("user", "", time() - 3600);
}