php - Why isn't my session variable being set? -


i've encountered problem while programming log-in script. take @ code:

<?php if(isset($submit)){     require 'db/connect.php';     $sql = "select id users password='$password' , email='$email'";     $entered_user = mysql_query($sql);     $num_rows = mysql_num_rows($entered_user);      $errors = array();      if( $num_rows != 1 )     {         $errors[] = '-account not exist ';     }     elseif( $num_rows == 1 )     {         session_start();         while($row = mysql_fetch_array($entered_user)){             $_session['id'] = $row['id'];             $_session['email'] = $email;             $_session['user'] = $row['username'];             $_session['pass'] = $password;             header('location: profile.php');             exit();         }     } }    ?> 

after user has submitted password , email, attempt store following in session variables: id, username, password , email. reason username not being stored , outputted on profile.php, redirected. here profile.php code:

<?php session_start(); ?> <html> <head>   <title>profile</title>   <link href='css/main.css' rel='stylesheet' /> </head> <body> <div id='container'>   <?php require 'include/header.php'; ?>    <?= 'njm id # ==>'.$_session['id'].'<br />'.'username ==>'.$_session['user'].'<br/>'.'password ==>'.$_session['pass'] ?>    <?php require 'include/footer.php'; ?>   </div> </div> </body> </html>  

if knows how fix issue please help. have learned html, css, js , php 2 months ago beginner :)

i've got few remarks on code fix issues.

  1. sessions should started before of other code executed. prevent strange problems arising.

  2. check if session isn't set.

recommended way versions of php >= 5.4.0

if ( session_status() == php_session_none ) {     session_start(); } 

for versions of php < 5.4.0

if (session_id() == '') {     session_start(); } 
  1. use mysqli_* or pdo queries. mysql_* deprecated , unsafe.

  2. your query isn't fetchin username

change:

$sql = "select id users password='$password' , email='$email'"; 

to:

$sql = "select id id, username username users password='".$password."' , email='".$email."'"; 

Comments