PHP Session not holding values
<p>After a good few hours of looking at posts and different forums I finally give up.
I have been learning PHP for the last 24 hours by trying to create a registration and a login page.
Registration seems to be working (I am sure that there are some bugs etc, but as of right now everything seems to be in sql).
As far as my login page, this is where I am having some problems.</p>

<h2>NEW EDIT</h2>

<p>Here is my registration.php</p>

<pre><code>&lt;?php
error_reporting(E_ALL);
ini_set('display_errors', '1');

//Set error msg to blank
$errorMsg = "";
// Check to see if the form has been submitted
if (isset($_POST['username'])) 
{
include_once 'db_connect.php';
$username = preg_replace('/[^A-Za-z0-9]/', '', $_POST['username']);
$password = preg_replace('/[^A-Za-z0-9]/', '', $_POST['password']); 
$accounttype = preg_replace('/[^A-Za-z]/','', $_POST['accounttype']);
$email = filter_var($_POST['email'], FILTER_VALIDATE_EMAIL);
//validate email with filter_var
if ((!$username) || (!$password) || (!$accounttype) || (!$email))
    {
    $errorMsg = "Everything needs to be filled out";
    } 
    else {
// if fields are not empty

// check if user name is in use
    $db_username_check = mysql_query("SELECT id FROM members WHERE          username='$username' LIMIT 1");
    $username_check = mysql_num_rows($db_username_check); 
// check if email is in use
    $db_email_check = mysql_query("SELECT id FROM members WHERE email='$email' LIMIT 1");
    $email_check = mysql_num_rows($db_email_check);

//if username is in use ... ERROR
    if ($username_check &gt; 0)  { 
    $errorMsg = "ERROR: username is already in use";

// if username is ok check if email is in use       
    } else if ($email_check &gt; 0) {
    $errorMsg = "ERROR: email is already in use"; 

    } else {

    session_start();
    $hashedPass = md5($password); 
// Add user info into the database table, claim your fields then values 
    $sql = mysql_query("INSERT INTO members (username, password, email, accounttype ) 
    VALUES('$username', '$hashedPass', '$email', '$accounttype')") or die (mysql_error());
    // Retrieves the ID generated for an AUTO_INCREMENT column by the previous query
    $id = mysql_insert_id();
    $_SESSION['id'] = $id; 
    mkdir("members/$id", 0755); 
    header("location: member_profile.php?id=$id");
    $errorMsg = "Registration Successful";
    exit();}

    }
// if the form has not been submitted
} else { $errorMsg = 'To register please fill out the form'; }
?&gt;
</code></pre>

<hr>

<p>here's my Login.php</p>

<pre><code>&lt;?php
 error_reporting(E_ALL);
    ini_set('display_errors', '1');

    // if the form has been submitted
    $errorMsg = "";
    if ($_POST['username']){

            include_once('db_connect.php');
            $username = stripslashes($_POST['username']);
            $username = strip_tags($username);
            $username = mysql_real_escape_string($_POST['username']);
            $password = mysql_real_escape_string($_POST['password']); 
            $hashedPass = md5($password);


            $sql = "SELECT username,password FROM members WHERE username ='$username' AND password = '$hashedPass'";
            $login_check = mysql_query($sql);
            $count = mysql_num_rows($login_check);
            $row = mysql_fetch_array($login_check);
            //var_dump($id, $username, $password);

                if($count==1)
                    {
                        session_start();
                        //$id = $row["id"]; 
                        //                $_SESSION['id'] = $userid; 
                        //                $username = $row['username'];  
                        //                $_SESSION['username'] = $username; 
                        //                header("location: member_profile.php?id=$userid"); 
                         echo "User name OK";
                         return true;
        } else {
            echo "Wrong username or password";
            return false;

                    }
                }

    ?&gt;
</code></pre>

<hr>

<p>Whenever someone registers $id = mysql_insert_id();will pull the ID from the last query and start a $_SESSION['id']. However during a login right after if($count==1) I am completely lost. For some reason the name and the password is checked and does go through but the ID fails.
I did try adding "SELECT id FROM members WHERE id='$id'" but my $id is always undefined. </p>

<p>My member_profile.php is something like this:</p>

<pre><code>&lt;?php
session_start();
$toplinks = "";
if(isset($_SESSION['id'])) {
//If the user IS logged in show this menu
$userid = $_SESSION['id'];
$username = $_SESSION['username'];
$toplinks = '
&lt;a href="member_profile.php?id=' .$userid. '"&gt; Profile &lt;/a&gt; &amp;bull;
&lt;a href="member_account.php"&gt;Account&lt;/a&gt; &amp;bull;
&lt;a href="logout.php"&gt;Logout&lt;/a&gt;
';

} else {
// If the user IS NOT logged in show this menu
$toplinks = '
&lt;a href="register.php"&gt;JOIN&lt;/a&gt; &amp;bull;
&lt;a href="login.php"&gt;LOGIN&lt;/a&gt;
';

}

?&gt;
</code></pre>

<p>Thank you to everyone for any tips as far as security, structure and coding style. This is day #3 of php for me.</p>

<p>Please excuse any errors.</p>
