Jump to content

another thing...$_COOKIE


Neptunus Maris

Recommended Posts

i cant get my cookies to work now

 

login code

 

if (!isset($_POST[submit])) {
$show_form = "yes";
} elseif (isset($_POST[submit])) {
$query =  "SELECT * FROM admins WHERE f_name = '$_POST[f_name]' AND l_name = '$_POST[l_name]' AND password = '$_POST[password]'";
$result = mysql_query($query) or die(mysql_error());
$check_row = mysql_num_rows($result);
if ($check_row != 1) {
	$error = "This Administration record is not found.  And/Or name and password do not match.";
	$show_form = "yes";
} else {
	$row = mysql_fetch_array($result) or die(mysql_error());
	if ($row[auth_lvl] == "SA") {
		$cookie = $row[adminID];
		setcookie("sa", $cookie, time()+1800, "/", "localhost", 1);
		$display = "You are logged in <a href = 'control_panel.php'>Go to Control Panel</a>.";
	} elseif ($row[auth_lvl] == "GM") {
		$cookie = $row[adminID];
		setcookie("admin", $cookie, time()+1800, "/", "localhost", 1);
		$display = "You are logged in <a href = 'control_panel.php'>Go to Control Panel</a>.";
	}
}
} //end if

 

next code : which is set on an administration page to check to see if the Administration cookies are set.

 

<?php
//Include mklib.php and connect to the database
include "../engine/mklib.php";
connect();

//==============
//Start the code
//==============

if (!isset($_COOKIE[admin])) {
header("Location: ../index.php");
exit;
} elseif (!isset($_COOKIE[sa])) {
header("Location: ../index.php");
exit;
} //end if

 

ok i try to login...it doesnt set the cookie...then when i take the code that checks the cookie the page works...what the heck...

 

please help....thank you!

Link to comment
https://forums.phpfreaks.com/topic/40107-another-thing_cookie/
Share on other sites

ok i have rewritten that code that sets the cookie

 

//Include mklib.php and main_style.css and connect to the database
include "../engine/mklib.php";
connect();

//Start Code

if (!isset($_POST[submit])) {
$show_form = "yes";
} elseif (isset($_POST[submit])) {
$query =  "SELECT * FROM admins WHERE f_name = '$_POST[f_name]' AND l_name = '$_POST[l_name]' AND password = '$_POST[password]'";
$result = mysql_query($query) or die(mysql_error());
$check_row = mysql_num_rows($result);
if ($check_row != 1) {
	$error = "This Administration record is not found.  And/Or name and password do not match.";
	$show_form = "yes";
} else {
	$row = mysql_fetch_array($result) or die(mysql_error());
	$value = $row[adminID];
	if ($row[auth_lvl] == "SA") {
		setcookie("sa", $value, time()+1800, "/", "rdrvision.com", 0);
		$display = "You are logged in <a href = 'control_panel.php'>Go to Control Panel</a>.";
	} elseif ($row[auth_lvl] == "GM") {
		setcookie("admin", $value, time()+1800, "/", "rdrvision.com", 0);
		$display = "You are logged in <a href = 'control_panel.php'>Go to Control Panel</a>.";
	}
}
} //end if

Link to comment
https://forums.phpfreaks.com/topic/40107-another-thing_cookie/#findComment-194061
Share on other sites

The problem is'nt in the cookie, it has been set right, the problem is in the information you're trying to check before setting the cookie - you've added the line:

$row = mysql_fetch_array($result) or die(mysql_error());

 

But you're forgetting the "while" statement to create the area that information can be used it, try something like this:

//Include mklib.php and main_style.css and connect to the database
include "../engine/mklib.php";
connect();

//Start Code

if (!isset($_POST[submit])) {
$show_form = "yes";
} elseif (isset($_POST[submit])) {
$query =  "SELECT * FROM admins WHERE f_name = '$_POST[f_name]' AND l_name = '$_POST[l_name]' AND password = '$_POST[password]'";
$result = mysql_query($query) or die(mysql_error());
$check_row = mysql_num_rows($result);
if ($check_row != 1) {
	$error = "This Administration record is not found.  And/Or name and password do not match.";
	$show_form = "yes";
} else {

  while ($row = mysql_fetch_array($result, MYSQL_ASSOC)) {
	$value = $row[adminID];
	if ($row[auth_lvl] == "SA") {
		setcookie("sa", $value, time()+1800, "/", "rdrvision.com", 0);
		$display = "You are logged in <a href = 'control_panel.php'>Go to Control Panel</a>.";
	} elseif ($row[auth_lvl] == "GM") {
		setcookie("admin", $value, time()+1800, "/", "rdrvision.com", 0);
		$display = "You are logged in <a href = 'control_panel.php'>Go to Control Panel</a>.";
	}
  }
  mysql_free_result($result);
}
} //end if

 

Otherwise it looks fine.

 

 

 

Link to comment
https://forums.phpfreaks.com/topic/40107-another-thing_cookie/#findComment-194184
Share on other sites

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.