Jump to content

Session variables not being set


DaveLinger

Recommended Posts

$query="SELECT * FROM users WHERE username = '$username'";
$result = mysql_query($query);
if(mysql_num_rows($result) < 1){echo "<p style=\"color:red\">Incorrect Email/Password Combination.</p>";}else{

while($row = mysql_fetch_array($result)){

if(md5($password) == $row['password']){
$_SESSION['uid'] = $row['uid'];
$_SESSION['username'] = $row['username'];
$_SESSION['password'] = $row['password'];
$_SESSION['email'] = $row['email'];
$_SESSION['ip'] = $row['ip'];

echo "<br /><p style=\"color:green\">Login successful.</p>";
}else{echo "<p style=\"color:red\">Incorrect Email/Password Combination.</p>";}
}

 

session_start() and sql connection are done before this code. SQL query does work - but now if after login I try to retrieve these variables, they aren't there.

Link to comment
Share on other sites

Might be an idea to check that anything is beng pulled from the database by echo'ing them to the browser.

 

If nothing is being pulled then not session variables are being set!

 

Also, if you're pulling empty data from the database then the session variables are being set to nothing.

Link to comment
Share on other sites

here's the whole page code:

 

<?php

include('includes/header.php');

$username = $_POST['username'];
$password = $_POST['password'];

if(isset($_POST['username']) || isset($_POST['password'])){

if (!$link = mysql_connect($sqlserver, $sqluser, $sqlpassword)) {
echo 'Could not connect to mysql';
exit;
}

if (!mysql_select_db($sqldb, $link)) {
echo 'Could not select database';
exit;
}

$query="SELECT * FROM users WHERE username = '$username'";
$result = mysql_query($query);
if(mysql_num_rows($result) < 1){echo "<p style=\"color:red\">Incorrect Email/Password Combination.</p>";}else{

while($row = mysql_fetch_array($result)){

if(md5($password) == $row['password']){
$_SESSION['uid'] = $row['uid'];
$_SESSION['username'] = $row['username'];
$_SESSION['password'] = $row['password'];
$_SESSION['email'] = $row['email'];
$_SESSION['ip'] = $row['ip'];

echo "<br /><p style=\"color:green\">Login successful.</p>";
}else{echo "<p style=\"color:red\">Incorrect Email/Password Combination.</p>";}
}

}
}

if($_GET['logout'] == "true"){
session_destroy();
echo "<p>Logged Out.</p>";
}

echo "
<h2>Login</h2>
<form method=\"post\" action=\"".$_SERVER['PHP_SELF']."\">
	<p>Username:<br /><input type=\"text\" name=\"username\" size=\"30\" /></p>
	<p>Password:<br /><input type=\"password\" name=\"password\" size=\"30\" /></p>
	<p><input type=\"submit\" value=\"Log In\" /></p>
</form>
";

include('includes/footer.php');

?>

Link to comment
Share on other sites

do you have cookies turned on in your browser? (i know, dumb question, but i had to ask)

 

have you tested to see if session work at all?

 

page1.php

<?php
session_start();
$_SESSION['test'] = "it works";
?>

 

page2.php

<?php
session_start();
echo $_SESSION['test'];
?>

Link to comment
Share on other sites

Yes, I've tested that sessions work on other areas of my site, and it can't be the if(md5($password) == $row['password']) code, because that part IS working. If the password I enter IS the same as in the DB, it says Login successful, and if I enter the wrong password, it says invalid.

Link to comment
Share on other sites

Still not working. I found out that if I change the session code to:

 

$_SESSION['uid'] = "test";
$_SESSION['username'] = "test";
$_SESSION['password'] = "test";
$_SESSION['email'] = "test";
$_SESSION['ip'] = "test";

 

It works, but if I have even one variable being set with $row[''], NO session variables are set.

Link to comment
Share on other sites

Alright here is a shot in the dark, if your query should only return 1 row why put it into a while loop? This could be where the issue comes into play.

 

<?php

include('includes/header.php');

$username = $_POST['username'];
$password = $_POST['password'];

if(isset($_POST['username']) || isset($_POST['password'])){

if (!$link = mysql_connect($sqlserver, $sqluser, $sqlpassword)) {
echo 'Could not connect to mysql';
exit;
}

if (!mysql_select_db($sqldb, $link)) {
echo 'Could not select database';
exit;
}

$query="SELECT * FROM users WHERE username = '$username'";
$result = mysql_query($query);
if(mysql_num_rows($result) < 1){echo "<p style=\"color:red\">Incorrect Email/Password Combination.</p>";}else{

$row = mysql_fetch_array($result);
if(md5($password) == $row['password']){
$_SESSION['uid'] = $row['uid'];
$_SESSION['username'] = $row['username'];
$_SESSION['password'] = $row['password'];
$_SESSION['email'] = $row['email'];
$_SESSION['ip'] = $row['ip'];

echo "<br /><p style=\"color:green\">Login successful.</p>";
}else{echo "<p style=\"color:red\">Incorrect Email/Password Combination.</p>";}

}
}

if($_GET['logout'] == "true"){
session_destroy();
echo "<p>Logged Out.</p>";
}

echo "
<h2>Login</h2>
<form method=\"post\" action=\"".$_SERVER['PHP_SELF']."\">
	<p>Username:<br /><input type=\"text\" name=\"username\" size=\"30\" /></p>
	<p>Password:<br /><input type=\"password\" name=\"password\" size=\"30\" /></p>
	<p><input type=\"submit\" value=\"Log In\" /></p>
</form>
";

include('includes/footer.php');

?>

 

As long as usernames are unique, there is no need for the while, and it could be overriding the good setting. Try that.

Link to comment
Share on other sites

Alright here is a shot in the dark, if your query should only return 1 row why put it into a while loop? This could be where the issue comes into play.

 

<?php

include('includes/header.php');

$username = $_POST['username'];
$password = $_POST['password'];

if(isset($_POST['username']) || isset($_POST['password'])){

if (!$link = mysql_connect($sqlserver, $sqluser, $sqlpassword)) {
echo 'Could not connect to mysql';
exit;
}

if (!mysql_select_db($sqldb, $link)) {
echo 'Could not select database';
exit;
}

$query="SELECT * FROM users WHERE username = '$username'";
$result = mysql_query($query);
if(mysql_num_rows($result) < 1){echo "<p style=\"color:red\">Incorrect Email/Password Combination.</p>";}else{

$row = mysql_fetch_array($result);
if(md5($password) == $row['password']){
$_SESSION['uid'] = $row['uid'];
$_SESSION['username'] = $row['username'];
$_SESSION['password'] = $row['password'];
$_SESSION['email'] = $row['email'];
$_SESSION['ip'] = $row['ip'];

echo "<br /><p style=\"color:green\">Login successful.</p>";
}else{echo "<p style=\"color:red\">Incorrect Email/Password Combination.</p>";}

}
}

if($_GET['logout'] == "true"){
session_destroy();
echo "<p>Logged Out.</p>";
}

echo "
<h2>Login</h2>
<form method=\"post\" action=\"".$_SERVER['PHP_SELF']."\">
	<p>Username:<br /><input type=\"text\" name=\"username\" size=\"30\" /></p>
	<p>Password:<br /><input type=\"password\" name=\"password\" size=\"30\" /></p>
	<p><input type=\"submit\" value=\"Log In\" /></p>
</form>
";

include('includes/footer.php');

?>

 

As long as usernames are unique, there is no need for the while, and it could be overriding the good setting. Try that.

 

Same result. Thanks for the effort.

Link to comment
Share on other sites

I just saw this thread, so here's my suggestion... Simplify the code some more by adding the password check onto the query. I would also use the function mysql_fetch_assoc() instead of mysql_fetch_array().

 

<?php

include('includes/header.php');

$username = $_POST['username'];
$password = $_POST['password'];

if(isset($_POST['username']) || isset($_POST['password'])){

     if (!$link = mysql_connect($sqlserver, $sqluser, $sqlpassword)) {
        echo 'Could not connect to mysql';
        exit;
     }

     if (!mysql_select_db($sqldb, $link)) {
        echo 'Could not select database';
        exit;
     }

     $query="SELECT * FROM users WHERE username = '$username' and `password` = '" . md5($password) . "'";
     $result = mysql_query($query) or die ("Problem with the query <pre>$query</pre><br>" . mysql_error());
     if(mysql_num_rows($result) < 1)
         echo '<p style="color:red">Incorrect Email/Password Combination.</p>';
     else {
         $row = mysql_fetch_assoc($result);
         $_SESSION['uid'] = $row['uid'];
         $_SESSION['username'] = $row['username'];
         $_SESSION['password'] = $row['password'];
         $_SESSION['email'] = $row['email'];
         $_SESSION['ip'] = $row['ip'];
         echo '<br /><p style="color:green">Login successful.</p>';
     }

     if($_GET['logout'] == "true"){
  session_destroy();
  echo "<p>Logged Out.</p>";
     }
}
echo "
<h2>Login</h2>
<form method=\"post\" action=\"".$_SERVER['PHP_SELF']."\">
	<p>Username:<br /><input type=\"text\" name=\"username\" size=\"30\" /></p>
	<p>Password:<br /><input type=\"password\" name=\"password\" size=\"30\" /></p>
	<p><input type=\"submit\" value=\"Log In\" /></p>
</form>
";

include('includes/footer.php');

?>

 

Note: I may have deleted one too many curly brackets...

 

BTW, indentation of your code can be very helpful for finding errors.

 

Ken

Link to comment
Share on other sites

I just saw this thread, so here's my suggestion... Simplify the code some more by adding the password check onto the query. I would also use the function mysql_fetch_assoc() instead of mysql_fetch_array().

 

<?php

include('includes/header.php');

$username = $_POST['username'];
$password = $_POST['password'];

if(isset($_POST['username']) || isset($_POST['password'])){

     if (!$link = mysql_connect($sqlserver, $sqluser, $sqlpassword)) {
        echo 'Could not connect to mysql';
        exit;
     }

     if (!mysql_select_db($sqldb, $link)) {
        echo 'Could not select database';
        exit;
     }

     $query="SELECT * FROM users WHERE username = '$username' and `password` = '" . md5($password) . "'";
     $result = mysql_query($query) or die ("Problem with the query <pre>$query</pre><br>" . mysql_error());
     if(mysql_num_rows($result) < 1)
         echo '<p style="color:red">Incorrect Email/Password Combination.</p>';
     else {
         $row = mysql_fetch_assoc($result);
         $_SESSION['uid'] = $row['uid'];
         $_SESSION['username'] = $row['username'];
         $_SESSION['password'] = $row['password'];
         $_SESSION['email'] = $row['email'];
         $_SESSION['ip'] = $row['ip'];
         echo '<br /><p style="color:green">Login successful.</p>';
     }

     if($_GET['logout'] == "true"){
  session_destroy();
  echo "<p>Logged Out.</p>";
     }
}
echo "
<h2>Login</h2>
<form method=\"post\" action=\"".$_SERVER['PHP_SELF']."\">
	<p>Username:<br /><input type=\"text\" name=\"username\" size=\"30\" /></p>
	<p>Password:<br /><input type=\"password\" name=\"password\" size=\"30\" /></p>
	<p><input type=\"submit\" value=\"Log In\" /></p>
</form>
";

include('includes/footer.php');

?>

 

Note: I may have deleted one too many curly brackets...

 

BTW, indentation of your code can be very helpful for finding errors.

 

Ken

 

Still does not work. If anyone would like to give this a try on their own server, here's the contents of config.php:

 

<?php

$absurl = "http://pizza.davelinger.com/";
$sitename = "Pizza Review";

$sqlserver = "localhost";
$sqluser = "my sql username";
$sqlpassword = "my sql password";
$sqldb = "my sql db";

session_start();

?>

 

and the db is just:

 

id (int(11) auto increment)

username (text)

password (text)

email (text)

regdate (date)

ip (text)

Link to comment
Share on other sites

<?php

session_start();

include('includes/header.php');

 

$username = $_POST['username'];

$password = $_POST['password'];

 

if(isset($_POST['username']) && isset($_POST['password'])){

 

    if (!mysql_connect($sqlserver, $sqluser, $sqlpassword)) {

        echo 'Could not connect to mysql';

        exit;

    }

 

    if (!mysql_select_db($sqldb, $link)) {

        echo 'Could not select database';

        exit;

    }

 

    $query="SELECT * FROM users WHERE username = '".$username."' and password = '" . md5($password) . "'";

    $result = mysql_query($query) or die ("Problem with the query <pre>$query</pre><br>" . mysql_error());

    if(mysql_num_rows($result) <= 0){

        echo '<p style="color:red">Incorrect Email/Password Combination.</p>';

}

    else {

        $row = mysql_fetch_assoc($result);

        $_SESSION['uid'] = $row['uid'];

        $_SESSION['username'] = $row['username'];

        $_SESSION['password'] = $row['password'];

        $_SESSION['email'] = $row['email'];

        $_SESSION['ip'] = $row['ip'];

        echo '<br /><p style="color:green">Login successful.</p>';

    }

 

    if($_GET['logout'] != " "){

  session_destroy();

  echo "<p>Logged Out.</p>";

    }

}

echo "

<h2>Login</h2>

<form method=\"post\" action=\"".$_SERVER['PHP_SELF']."\">

<p>Username:<br /><input type=\"text\" name=\"username\" size=\"30\" /></p>

<p>Password:<br /><input type=\"password\" name=\"password\" size=\"30\" /></p>

<p><input type=\"submit\" value=\"Log In\" /></p>

</form>

";

 

include('includes/footer.php');

 

?>

 

i edit some of the lines pls try this one

Link to comment
Share on other sites

teng84, you did not define $link as I did in my code, so yours failed. Also, even if I add the definition of $link, it says "Logged In Successfully" "Logged Out". Your if statement for logout goes every time because $_GET['logout'] is never equal to " ".

Link to comment
Share on other sites

Here is YOUR code, revised:

 

<?php
session_start();
include('includes/header.php');

$username = $_POST['username'];
$password = $_POST['password'];

if(isset($_POST['username']) && isset($_POST['password'])){

     if (!$link = mysql_connect($sqlserver, $sqluser, $sqlpassword)) {
        echo 'Could not connect to mysql';
        exit;
     }

     if (!mysql_select_db($sqldb, $link)) {
        echo 'Could not select database';
        exit;
     }

     $query="SELECT * FROM users WHERE username = '".$username."' and password = '" . md5($password) . "'";
     $result = mysql_query($query) or die ("Problem with the query

$query


" . mysql_error());
     if(mysql_num_rows($result) <= 0){
         echo '<p style="color:red">Incorrect Email/Password Combination.</p>';
    }
     else {
         $row = mysql_fetch_assoc($result);
         $_SESSION['uid'] = $row['uid'];
         $_SESSION['username'] = $row['username'];
         $_SESSION['password'] = $row['password'];
         $_SESSION['email'] = $row['email'];
         $_SESSION['ip'] = $row['ip'];
         echo '
<p style="color:green">Login successful.</p>';
     }
}

     if($_GET['logout'] != ""){
     session_destroy();
     echo "<p>Logged Out.</p>";
     }
    
echo "
<h2>Login</h2>
   <form method=\"post\" action=\"".$_SERVER['PHP_SELF']."\">
      <p>Username:
<input type=\"text\" name=\"username\" size=\"30\" /></p>
      <p>Password:
<input type=\"password\" name=\"password\" size=\"30\" /></p>
      <p><input type=\"submit\" value=\"Log In\" /></p>
   </form>
";

include('includes/footer.php');

?>

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • 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.