PHP - MySQL Undefined _SESSION

This is my code, from a tutorial website. It is meant to log a user in and set their data into SESSION variable. But it does not return the final echo.

[php]

<?php //signin.php include 'connect.php'; include 'header.php'; echo '

Sign in

'; //first, check if the user is already signed in. If that is the case, there is no need to display this page if(isset($_SESSION['signed_in']) && $_SESSION['signed_in'] == true) { echo 'You are already signed in, you can sign out if you want.'; } else { if($_SERVER['REQUEST_METHOD'] != 'POST') { /*the form hasn't been posted yet, display it note that the action="" will cause the form to post to the same page it is on */ echo ' Username: Password: '; } else { /* so, the form has been posted, we'll process the data in three steps: 1. Check the data 2. Let the user refill the wrong fields (if necessary) 3. Varify if the data is correct and return the correct response */ $errors = array(); /* declare the array for later use */ if(!isset($_POST['user_name'])) { $errors[] = 'The username field must not be empty.'; } if(!isset($_POST['user_pass'])) { $errors[] = 'The password field must not be empty.'; } if(!empty($errors)) /*check for an empty array, if there are errors, they're in this array (note the ! operator)*/ { echo 'Uh-oh.. a couple of fields are not filled in correctly..'; echo '
    '; foreach($errors as $key => $value) /* walk through the array so all the errors get displayed */ { echo '
  • ' . $value . '
  • '; /* this generates a nice error list */ } echo '
'; } else { include 'connect.php'; if(!mysqli_connect($server, $username, $password, $database)) { $sql = "SELECT user_id, user_name, user_level FROM users WHERE user_name = '" . ($_POST['user_name']) . "' AND user_pass = '" . sha1($_POST['user_pass']) . "'"; $result = mysqli_query($mysqli , $sql); if(!$result) { echo 'Something went wrong while signing in. Please try again later.'; echo mysql_error(); //debugging purposes, uncomment when needed } else { //the query was successfully executed, there are 2 possibilities //1. the query returned data, the user can be signed in //2. the query returned an empty result set, the credentials were wrong if(mysql_num_rows($result) == 0) { echo 'You have supplied a wrong user/password combination. Please try again.'; } else { //set the $_SESSION['signed_in'] variable to TRUE $_SESSION['signed_in'] = true; //we also put the user_id and user_name values in the $_SESSION, so we can use it at various pages while($row = mysql_fetch_assoc($result)) { $_SESSION['user_id'] = $row['user_id']; $_SESSION['user_name'] = $row['user_name']; $_SESSION['user_level'] = $row['user_level']; } echo 'Welcome, ' . $_SESSION['user_name'] . '. Proceed to the forum overview.'; } } } } } } include 'footer.php'; ?>

[/php]

It looks like you’re missing session_start();

Put it right after the opening <?php
[php]<?php
session_start();
//signin.php
// …
[/php]

Does this login code work fine?

If so, do you want if I use it? been after a working login code for a while

Hasn’t worked.

Sorry it doesn’t work.

Just a little thing to try is keep putting echos up each line until you get to the line that is at fault

Put this after the <?php to see if their are any errors

[php]error_reporting(E_ALL);
ini_set(“display_errors”, 1);[/php]

Doesn’t return any errors on the page.

Can you post all the output you do get?

The output I get is : Notice: Undefined index: signed_in in C:\xampp\htdocs\header.php on line 22

Header.php:

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN"
"http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" xml:lang="nl" lang="nl">
<head>
    <meta http-equiv="Content-Type" content="text/html; charset=UTF-8" />
    <meta name="description" content="A short description." />
    <meta name="keywords" content="put, keywords, here" />
    <title>PHP-MySQL forum</title>
    <link rel="stylesheet" href="style.css" type="text/css">
</head>
<body>
<h1>Social.3-a.net</h1>
    <div id="wrapper">
    <div id="menu">
        <a class="item" href="index.php">Home</a> -
        <a class="item" href="create_topic.php">Create a topic</a> -
        <a class="item" href="create_cat.php">Create a category</a>


    <div>
    <?php
    if($_SESSION['signed_in'])
     {
      echo 'Hello' . $_SESSION['user_name'] . '. Not you? <a href="signout.php">Sign out</a>';
      }
     else
     {
      echo '<a href="signin.php">Sign in</a> or <a href="signup.php">create an account</a>.';
}
 ?>
Sponsor our Newsletter | Privacy Policy | Terms of Service