0

I tried to create a login script using some functions but session not work

this is my code

if(isset($_POST['submit'])){
  $username = $_POST["username"];
  $password = md5($_POST["password"]);

  if(login($username, $password)){
    $_SESSION['id'] == $row['ID'];
    redirect_to('index.php');
    exit();
  }
}

And i create this function named login

function login($user, $pass) {

    // Sanitize user input
    $user = strtolower(sanitize_input($user));
    $pass = sanitize_input($pass);

    // Build database query
    $sql = "select * from users where username = '".$user."' and password = '".$pass."' limit 1";
    $row = execute_query($sql);
    if($user == strtolower($row['username']) && $pass == $row['password']){
      return true;
    }else{
      return false;
    }
}

And this is another function named execut_query

function execute_query($sql) {

    // Initialize dataset
    $dataset = FALSE;

    // Execute database query
    $result = @execute_statement($sql);

    // Count number of rows
    $count = @mysql_num_rows($result);

    // Check for single row returned
    if ($count == 1) {

        // Fetch a single row from database cursor
        $dataset = @mysql_fetch_assoc($result);
    }

    // Check for multiple rows returned
    if ($count > 1) {

        // Initialize rows array
        $dataset = array();

        // Fetch all rows from database cursor
        while ($row = @mysql_fetch_assoc($result)) {
            $dataset[] = $row;
        }
    }

    // Return dataset
    return $dataset;
}

Whene i press submit login form session not set

6
  • session_start() ? php.net/manual/en/function.session-start.php Commented Aug 15, 2015 at 2:56
  • i started function file with this: session_start(); ob_start(); Commented Aug 15, 2015 at 2:57
  • 2
    Just a quick note: should be $_SESSION['id'] = $row['ID']; not $_SESSION['id'] == $row['ID']; Commented Aug 15, 2015 at 2:58
  • 1
    AS @Tim said, use "=" not "==". "=" is for assigning a value, "==" is for comparing two values. Commented Aug 15, 2015 at 2:59
  • i edit it but it`s still not work Commented Aug 15, 2015 at 3:00

0

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.