-2

I have this code that works fine on a development machine but when ported over to production the user will get the alert wrong details despite being right.

My guess is this is a session issue with PHP. However, I am seeing session files saved to /tmp on the server. Am I missing something obvious as to what is causing this issue?

The code is referenced below.

session_start();
include_once 'dbconnect.php';

if(isset($_SESSION['user'])!="")
    {
        header("Location: home.php");
    }
if(isset($_POST['btn-login']))
    {
        $email = mysql_real_escape_string($_POST['email']);
        $upass = mysql_real_escape_string($_POST['pass']);
        $res=mysql_query("SELECT * FROM `USERS` WHERE `EMAIL` = '$email'");
        $row=mysql_fetch_array($res);
        if($row['PASSWORD']==md5($upass))
            {
                $_SESSION['user'] = $row['ID'];
                header("Location: home.php");
            }
        else
            {
                ?>
                <script>alert('wrong details');</script>
                <?php
            }

    }
Barmar
  • 741,623
  • 53
  • 500
  • 612
Jason
  • 811
  • 1
  • 12
  • 26

4 Answers4

1

Change

if (isset($_SESSION['user']) != "") {
    header("Location: home.php");
}

to

if (!empty($_SESSION['user'])) {
    header("Location: home.php");
}

For more information check out this post https://stackoverflow.com/a/1519849/1592783 by karim79. Or if you would like to know about empty check out http://php.net/empty

Community
  • 1
  • 1
Jack
  • 2,891
  • 11
  • 48
  • 65
  • many love it when an explanation is given. That way, everyone learns. You know what's going on and I know and others, but many who are new to conditional statements and PHP will not. *Food for thought* on your future answers. Tip: it attracts more votes(+). ;-) – Funk Forty Niner Dec 22 '15 at 17:09
1

Try this. I made the following changes:

  • Fix the isset() test of $_SESSION['user']. You shouldn't compare it to "". Perhaps what you really want is if(!empty($_SESSION['user']).

  • Use elseif, so that it only checks the email and password when the session variable isn't set.

session_start();
include_once 'dbconnect.php';

if(isset($_SESSION['user']))
    {
        header("Location: home.php");
    }
elseif(isset($_POST['btn-login']))
    {
        $email = mysql_real_escape_string($_POST['email']);
        $upass = mysql_real_escape_string($_POST['pass']);
        $res=mysql_query("SELECT * FROM `USERS` WHERE `EMAIL` = '$email'");
        $row=mysql_fetch_array($res);
        if($row['PASSWORD']==md5($upass))
            {
                $_SESSION['user'] = $row['ID'];
                header("Location: home.php");
            }
        else
            {
                ?>
                <script>alert('wrong details');</script>
                <?php
            }

    }
Barmar
  • 741,623
  • 53
  • 500
  • 612
  • Still same issue. Not entirely sure as to why it acts as it fails an md5 check. – Jason Dec 22 '15 at 17:06
  • Don't use `mysql_real_escape_string` on the password, since you're not substituting it into a query. – Barmar Dec 22 '15 at 17:17
  • Put `var_dump($row['PASSWORD'], md5($upass));` into the `else` clause so you can see what the password is. – Barmar Dec 22 '15 at 17:18
  • 1
    Better yet, don't use MD5 at all, but something more of "this century". – Funk Forty Niner Dec 22 '15 at 17:19
  • @Fred-ii- If the logic is wrong, it doesn't matter what password hash he uses. Let's get the logic right first. – Barmar Dec 22 '15 at 17:20
  • 1
    I'm surprised you didn't make a mention about their use of MD5. Had I put an answer in and not mentioning that and substituting it for a safe hashing function, people would be all over me. Edit: not to mention their use of `mysql_`. – Funk Forty Niner Dec 22 '15 at 17:22
1

The isset will return only true or false.

if(isset($_SESSION['user'])!="")

Try to change it to

if(isset($_SESSION['user']))

Or compare the value os the $_SESSION['user'] with:

if($_SESSION['user'] == "")
lwb
  • 379
  • 5
  • 17
0

So I finally fixed it. Going from Windows to Linux MySQL table names become case sensitive. Did not know that.

Jason
  • 811
  • 1
  • 12
  • 26
  • 1
    Yeah, * NIX and Windows are two different animals altogether, *everywhere*. Keep that in mind for folder/filename naming conventions also ;-) – Funk Forty Niner Dec 22 '15 at 17:16