0

May be there are already solutions of my problem but I don't know what the exact name of my problem. So I can't search them now and I need post my problem here.

When I log in manually(mean putting username & password with my hand), my profile page shows all data correctly from my database. See the below picture.... Correct image

Now I am logging out and again log in. This time I checked "Remember me box". Then I closed my browser without logging out. So next time I got access to my home page directly as cookies worked. All are fine till now. But the disaster come when I checked my profile page. See the below picture.... :( Incorrect image

I have used "Username" column to detect the row of table of my database. Because my code doesn't allow same username from another user. Here is the code of the profile page:

session_start();
$name = $_SESSION['username'];
$result = mysql_query("SELECT * FROM store WHERE Username='$name'");
while ($row = mysql_fetch_array($result)) {
    $first = $row['Firstname'];
    $last = $row['Lastname'];
    $use = $row['Username'];
    $pas = $row['Password'];
}

Then I just echo them (that four variables $first, $last, $use and $pas) in profile page. So what should I do now?

NullPoiиteя
  • 56,591
  • 22
  • 125
  • 143
Shahriar Kabir
  • 274
  • 1
  • 9
  • 26

2 Answers2

1

first make sure that session is set and not empty for that you cam use empty() it will explicit check isset()

session_start();
if (!empty($_SESSION['username'])) {
    $name = $_SESSION['username'];
    $result = mysql_query("SELECT * FROM store WHERE Username='$name'");
    while ($row = mysql_fetch_array($result)) {
        $first = $row['Firstname'];
        $last = $row['Lastname'];
        $use = $row['Username'];
        $pas = $row['Password'];
    }
}

NOTE 1 : your session is not secure you need to secure session for that there are some good read

  1. PHP Security Guide: Sessions
  2. Sessions and security
  3. PHP Session Security
  4. How safe are PHP session variables?

NOTE 2 : Use of mysql_* function are deprecated even it will generate E_DEPRECATED warning in php5.5 so use PDO or MySQLi instead

Community
  • 1
  • 1
NullPoiиteя
  • 56,591
  • 22
  • 125
  • 143
-2
session_start();
$name = $_SESSION['username']; // here is the problem
$result = mysql_query("SELECT * FROM store WHERE Username='$name'");
while ($row = mysql_fetch_array($result)) {
    $first = $row['Firstname'];
    $last = $row['Lastname'];
    $use = $row['Username'];
    $pas = $row['Password'];
}

you can see by checking that session is set or not by this statement

if (isset($_SESSION['username'])) {
    echo "Session is Set";
} else {
    echo "Session is not Set";
}

i think error is due to session because it will not be set!

NullPoiиteя
  • 56,591
  • 22
  • 125
  • 143
Muhammad Talha Akbar
  • 9,952
  • 6
  • 38
  • 62