Alright i am back.... After doing some renovating of my scripts and reading up a little more on PHP and MySQL I have run into another bump..... When I run the query it returns Resource id #5....
Here is the script w/ the query:
function user_login($user_name, $pass)
{
if (!$_POST['user_name'] || !$_POST['pass']) {
$feedback = 'ERROR--Missing username or password';
return $feedback;
} else {
$conn = db_connect();
$user_name = $_POST['user_name'];
$pass = $_POST['pass'];
$result = mysql_query("SELECT * FROM users
WHERE user_name = '$user_name'
AND pass = md5('$pass')");
$feedback = $result;
return $feedback;
if (!$result){
$feedback = 'ERROR--Database Error.';
return $feedback;
return false;
} else {
if (mysql_num_rows($result) == 1){
$is_confirmed = mysql_result($result, 0, 'is_confirmed');
if ($is_confirmed == '1'){
user_set_tokens($user_name);
return true;
} else {
$feedback = 'ERROR--You may not have confirmed your account yet.';
return $feedback;
}
} else {
$feedback = 'ERROR--User not found or password incorrect.';
return $feedback;
return false;
}
}
}
}
Now here is the script that is calling the function:
<?php require_once 'funcs/login_funcs.php'; ?>
<?php
if (!user_isloggedin()) {
//user_logout();
$_COOKIE['user_name'] = '';
}
if ($_POST['login']) {
if (strlen($_POST['username']) <= 25 &&
strlen($_POST['password']) <=25) {
$feedback = user_login($user_name, $pass);
} else {
$feedback = 'ERROR--Username and password are too long';
}
if ($feedback == 1) {
header("Location: home.php");
} else {
$feedback_str = "<p class=\"declined\">$feedback</p>";
}
} else {
$feedback_str = '';
}
$php_self = $_SERVER['SCRIPT_NAME'];
?>well, the problem is right here:
$result = mysql_query(" SELECT * FROM users
WHERE user_name = '$user_name'
AND pass = md5('$pass')");
$feedback = $result;
return $feedback; // this shouldn't be here!!!
you are ending the function with simply a resource id in your returning variable..
you may also want to look at all the other returns you have in there - you got one after the other in several places...ok but how do I print on screen the query results... I am trying to get the login page to work but it keeps telling me user not found....<?php
$result = mysql_query($sql);
while ($row = mysql_fetch_assoc($result))
{
echo $row['fieldname_1']
echo $row['fieldname_2'];
}
?>
Look up the functions mysql_fetch_assoc(), mysql_fetch_object(), mysql_fetch_row() on PHP.net (<!-- w --><a class="postlink" href="http://www.php.net">www.php.net</a><!-- w -->)ok now I am getting this error.......
Warning: mysql_num_rows(): supplied argument is not a valid MySQL result resource in /Library/WebServer/Documents/iguananet.com/funcs/login_funcs.php on line 66
This is the code that I added:
$num_results = mysql_num_rows($results);
for($i=0; $i < $num_results; $i++)
{
$row = mysql_fetch_row($result);
echo ($i+1).'.Results: ';
echo $row['test'];
}in the mysql_num_row() function you have passed it the result id called $results. I think it should just be $result (not plural) judging by what you have further down in your code.
Btw, you don't have to work out how many rows were returned and build a for loop to iterate through them. You can do what I showed and use a while loop. It will stop when it can no longer assign a returned row to the $row variable because it has reached the end. It's up to you though, your way is still perfectly valid.OK so now I have made those changes....
Nothing prints in the field and the error message that comes up is that the user was not found or the password is incorrect... I know the user is there and I know the password is correct so now what???Do you have a database field called Test? Because that's what you are asking it to print.
My guess is that:
echo $row['test'];
should read something like:
echo $row['user'];
echo $row['password'];
or whatever the field names are that you are trying to display the contents of. If you want a quick dump of everything in each record then use:
print_r($row);
instead of the echo statementsOK it turns out that for some reason the password is not being understood..... When I put the password into the database I used md5 encryption and I am using md5 encryption to take it out..... am I doing something wrong?????ok I fixed that problem....
But now I am having header trouble.....
Here are the errors I am getting.....
Warning: Cannot modify header information - headers already sent by (output started at /Library/WebServer/Documents/iguananet.com/funcs/login_funcs.php:5) in /Library/WebServer/Documents/iguananet.com/funcs/login_funcs.php on line 135
Warning: Cannot modify header information - headers already sent by (output started at /Library/WebServer/Documents/iguananet.com/funcs/login_funcs.php:5) in /Library/WebServer/Documents/iguananet.com/funcs/login_funcs.php on line 137
Warning: Cannot modify header information - headers already sent by (output started at /Library/WebServer/Documents/iguananet.com/funcs/login_funcs.php:5) in /Library/WebServer/Documents/iguananet.com/login.php on line 28Here is the script:
function user_logout() {
setcookie('user_name', '', (time()+2592000), '/', '', 0);
setcookie('id_hash', '', (time()+2592000), '/', '', 0);
}
function user_set_tokens($user_name_in) {
global $supersecret_hash_padding;
if (!$user_name_in) {
$feedback = 'ERROR--No username';
return false;
}
$user_name = strtolower($user_name_in);
$id_hash = md5($user_name.$supersecret_hash_padding);
//Line 135 is below
setcookie('user_name', $user_name, time() + (60 * 60 * 24), '/', '', 0);
//Line 137 is below
setcookie('id_hash', $id_hash, time() + (60 * 60 * 24), '/', '', 0);
}You cannot set cookies if anything has been output to the browswer already. In other words if you have already executed a print, echo, printf, or any other output statement then you cannot set cookies. Even blank lines at the top of the HTML document can cause this error.
Make sure you set your cookies first and then do any echo or printing after (or buffer the page and send it in one go using the php's ob* functions)Ok then telll me why the same script would work on another server????? Cause it works on one server but on the server it is supposed to be on it gives me the header error saying that i cnat modify headers cause they were already sent.... The server that it does work on is a Linux box and the server it does not work on is an Xserve(Mac).....it may have worked, but it shouldn't.
torrent is right - session and cookie variables must be set before any html output is sent to the client.ok so I am going to zip my scripts and can u guys tell me how they should be cause I am not sure how to set it up so that the cookies go first?????
Here is the script w/ the query:
function user_login($user_name, $pass)
{
if (!$_POST['user_name'] || !$_POST['pass']) {
$feedback = 'ERROR--Missing username or password';
return $feedback;
} else {
$conn = db_connect();
$user_name = $_POST['user_name'];
$pass = $_POST['pass'];
$result = mysql_query("SELECT * FROM users
WHERE user_name = '$user_name'
AND pass = md5('$pass')");
$feedback = $result;
return $feedback;
if (!$result){
$feedback = 'ERROR--Database Error.';
return $feedback;
return false;
} else {
if (mysql_num_rows($result) == 1){
$is_confirmed = mysql_result($result, 0, 'is_confirmed');
if ($is_confirmed == '1'){
user_set_tokens($user_name);
return true;
} else {
$feedback = 'ERROR--You may not have confirmed your account yet.';
return $feedback;
}
} else {
$feedback = 'ERROR--User not found or password incorrect.';
return $feedback;
return false;
}
}
}
}
Now here is the script that is calling the function:
<?php require_once 'funcs/login_funcs.php'; ?>
<?php
if (!user_isloggedin()) {
//user_logout();
$_COOKIE['user_name'] = '';
}
if ($_POST['login']) {
if (strlen($_POST['username']) <= 25 &&
strlen($_POST['password']) <=25) {
$feedback = user_login($user_name, $pass);
} else {
$feedback = 'ERROR--Username and password are too long';
}
if ($feedback == 1) {
header("Location: home.php");
} else {
$feedback_str = "<p class=\"declined\">$feedback</p>";
}
} else {
$feedback_str = '';
}
$php_self = $_SERVER['SCRIPT_NAME'];
?>well, the problem is right here:
$result = mysql_query(" SELECT * FROM users
WHERE user_name = '$user_name'
AND pass = md5('$pass')");
$feedback = $result;
return $feedback; // this shouldn't be here!!!
you are ending the function with simply a resource id in your returning variable..
you may also want to look at all the other returns you have in there - you got one after the other in several places...ok but how do I print on screen the query results... I am trying to get the login page to work but it keeps telling me user not found....<?php
$result = mysql_query($sql);
while ($row = mysql_fetch_assoc($result))
{
echo $row['fieldname_1']
echo $row['fieldname_2'];
}
?>
Look up the functions mysql_fetch_assoc(), mysql_fetch_object(), mysql_fetch_row() on PHP.net (<!-- w --><a class="postlink" href="http://www.php.net">www.php.net</a><!-- w -->)ok now I am getting this error.......
Warning: mysql_num_rows(): supplied argument is not a valid MySQL result resource in /Library/WebServer/Documents/iguananet.com/funcs/login_funcs.php on line 66
This is the code that I added:
$num_results = mysql_num_rows($results);
for($i=0; $i < $num_results; $i++)
{
$row = mysql_fetch_row($result);
echo ($i+1).'.Results: ';
echo $row['test'];
}in the mysql_num_row() function you have passed it the result id called $results. I think it should just be $result (not plural) judging by what you have further down in your code.
Btw, you don't have to work out how many rows were returned and build a for loop to iterate through them. You can do what I showed and use a while loop. It will stop when it can no longer assign a returned row to the $row variable because it has reached the end. It's up to you though, your way is still perfectly valid.OK so now I have made those changes....
Nothing prints in the field and the error message that comes up is that the user was not found or the password is incorrect... I know the user is there and I know the password is correct so now what???Do you have a database field called Test? Because that's what you are asking it to print.
My guess is that:
echo $row['test'];
should read something like:
echo $row['user'];
echo $row['password'];
or whatever the field names are that you are trying to display the contents of. If you want a quick dump of everything in each record then use:
print_r($row);
instead of the echo statementsOK it turns out that for some reason the password is not being understood..... When I put the password into the database I used md5 encryption and I am using md5 encryption to take it out..... am I doing something wrong?????ok I fixed that problem....
But now I am having header trouble.....
Here are the errors I am getting.....
Warning: Cannot modify header information - headers already sent by (output started at /Library/WebServer/Documents/iguananet.com/funcs/login_funcs.php:5) in /Library/WebServer/Documents/iguananet.com/funcs/login_funcs.php on line 135
Warning: Cannot modify header information - headers already sent by (output started at /Library/WebServer/Documents/iguananet.com/funcs/login_funcs.php:5) in /Library/WebServer/Documents/iguananet.com/funcs/login_funcs.php on line 137
Warning: Cannot modify header information - headers already sent by (output started at /Library/WebServer/Documents/iguananet.com/funcs/login_funcs.php:5) in /Library/WebServer/Documents/iguananet.com/login.php on line 28Here is the script:
function user_logout() {
setcookie('user_name', '', (time()+2592000), '/', '', 0);
setcookie('id_hash', '', (time()+2592000), '/', '', 0);
}
function user_set_tokens($user_name_in) {
global $supersecret_hash_padding;
if (!$user_name_in) {
$feedback = 'ERROR--No username';
return false;
}
$user_name = strtolower($user_name_in);
$id_hash = md5($user_name.$supersecret_hash_padding);
//Line 135 is below
setcookie('user_name', $user_name, time() + (60 * 60 * 24), '/', '', 0);
//Line 137 is below
setcookie('id_hash', $id_hash, time() + (60 * 60 * 24), '/', '', 0);
}You cannot set cookies if anything has been output to the browswer already. In other words if you have already executed a print, echo, printf, or any other output statement then you cannot set cookies. Even blank lines at the top of the HTML document can cause this error.
Make sure you set your cookies first and then do any echo or printing after (or buffer the page and send it in one go using the php's ob* functions)Ok then telll me why the same script would work on another server????? Cause it works on one server but on the server it is supposed to be on it gives me the header error saying that i cnat modify headers cause they were already sent.... The server that it does work on is a Linux box and the server it does not work on is an Xserve(Mac).....it may have worked, but it shouldn't.
torrent is right - session and cookie variables must be set before any html output is sent to the client.ok so I am going to zip my scripts and can u guys tell me how they should be cause I am not sure how to set it up so that the cookies go first?????