I tried transferring a working login system from one domain to another. It works perfectly on domain A, but starts giving errors:
Warning: Cannot modify header
information – headers already sent by
(output started at
/home/hootlute/public_html/personal/example/files/admin/config.php:9)
in
/home/hootlute/public_html/personal/example/files/admin/admin_process.php
on line 34Warning: mysql_free_result(): supplied
argument is not a valid MySQL result
resource in
/home/hootlute/public_html/personal/example/files/admin/admin_process.php
on line 48
Below is my code from admin_process
<?php
session_start();
if ($_POST['adminID']=="") {
$_SESSION['error']="Please log in with an admin account.";
header('Location: http://login.example.com/login/admin');
}
include 'config.php';
include 'functions.php';
$conn = mysql_connect($dbhost, $dbuser, $dbpass) or die ('Error connecting to mysql');
mysql_select_db($dbname);
$sql = "SELECT * FROM admin WHERE username='" .$_POST["adminID"]. "' AND password='" .$_POST["password"]. "'";
$result = mysql_query($sql);
if (!$result) {
die('Invalid query: ' . mysql_error());
}
$num_rows = mysql_num_rows($result);
// CHECKS FOR EXISTING ACCOUNT
if ($num_rows === 0) {
$_SESSION['error']="Unable to log in. Admin account not found";
printf("<script>location.href='http://login.example.com.com/login/'</script>");
}
else {
$admin_rows = mysql_fetch_assoc($result);
$cookie_info = $admin_rows['admin_id']."$".$admin_rows['name'];
$expire = time() + 9800;
setcookie("FS_admin_id",$cookie_info,$expire);
$sql = "UPDATE admin set login_time = UTC_TIMESTAMP() WHERE username='" .$_POST["adminID"]. "' AND password='" .$_POST["password"]. "'";
$logresult = mysql_query($sql);
if (!$logresult) {
die('Invalid query: ' . mysql_error());
}
/* printf("<script>location.href='admin_home.php'</script>"); */
}
mysql_free_result($result);
mysql_free_result($logresult);
mysql_close($conn);
?>
Line 34 = setcookie(“FS_admin_id”,$cookie_info,$expire);
What is wrong with the code? Is it because of the difference in PHP version ?
I also get the same type of errors when I declare session_start() in the middle of my codes… despite not having any data printed.
I’ve been searching for setcookie() problems but the results were all related to the usage of setcookie().
Any reply will be much appreciated 🙂
Let me answer here as well 😀
Check line 9 in config.php 🙂 I bet you have a trailing whitespace there.
EDIT:
To avoid this in future, just remove ALL final ?> lines from your PHP scripts. PHP can live without them 🙂 And also heed Lekensteyn’s advice re SQL Injection.