Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8386557
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 9, 20262026-06-09T17:58:22+00:00 2026-06-09T17:58:22+00:00

I’m having a lot of trouble with the $_SESSION variable. I’m trying to create

  • 0

I’m having a lot of trouble with the $_SESSION variable. I’m trying to create a way for users to log in and out. I can log a user in but i don’t seem to be able to maintain the session when i switch page. When the user correctly logs in they are taken to profile.php. But if i return to index.php the following error is printed:

Notice: Undefined index: login in /Applications/MAMP/htdocs/www/Shared sites/userlogreg/index.php on line 3

I’m quite new to this but from looking on SO and elsewhere i can’t seem to figure it out. Any help would be appreciated.

index.php

<?php
session_start();
if ($_SESSION['login'] == 1) {
    echo "<h1>Logged in!</h1>"; 
} else {
    echo "<h1>Not logged in</h1><br/>";
}

?>
<!DOCTYPE HTML>
<html>
<head>
    <title>Index page</title>
</head>
<body>
    <h2>Login</h2>
    <form action="login.php" method="POST">
        <div>
            <label for="emailSignIn">Email:</label>
            <input type="email" name="email" placeholder="Email" required="required" />
        </div>
        <div>
            <label for="passwordSignIn">Password:</label>
            <input type="password" name="password" placeholder="Password" required="required" />
        </div>
        <input type="submit" name="submit" value="Sign in" />
    </form> 

    <h2>Register</h2>
    <form action="register.php" method="POST">
        <div>
            <label for="firstnameRegister">First name:</label>
            <input type="text" name="firstname" placeholder="First name" required="required" />
        </div>
        <div>
            <label for="lastnameRegister">Last name:</label>
            <input type="text" name="lastname" placeholder="Last name" required="required" />
        </div>
        <div>
            <label for="emailRegister">Email:</label>
            <input type="email" name="email" placeholder="Email" required="required" />
        </div>
        <div>
            <label for="passwordRegister">Password:</label>
            <input type="password" name="password" placeholder="Password" required="required">
        </div>
        <input type="submit" name="submit" value="Create account" />
    </form>

</body>

</html>

login.php

<?php

$email = sanitize_input($_POST['email']); //echo "Sanitized email: ".$email; echo "<br/>";
$password = $_POST['password']; //echo "Inputted password: ".$password; echo "<br/>";

if ((!isset($email)) || (!isset($password))) {
    // VISITOR NEEDS TO ENTER AN EMAIL AND PASSWORD
    //echo "Data not provided";
} else {
    // CONNECT TO MYSQL
    $mysql = mysqli_connect("localhost", "root", "root");
    if(!$mysql) {
    //echo "Cannot connect to PHPMyAdmin.";
    exit;
    } else {

    }
}
// SELECT THE APPROPRIATE DATABASE
$selected = mysqli_select_db($mysql, "languageapp");
if(!$selected) {
    //echo "Cannot select database.";
    exit;
} else {

}

// GET THE USER'S UNIQUE SALT FROM THE DATABASE
$unique_salt = mysqli_query($mysql, "select uniqueSalt from user where email = '".$email."'");
$row = mysqli_fetch_array($unique_salt);
//echo "Salt: ".$row['uniqueSalt']; echo "<br/>";

// HASH THE PASSWORD 
$iterations = 10;
$hashed_password = crypt($password,$row['uniqueSalt']); 
for ($i = 0; $i < $iterations; ++$i)
{
    $hashed_password = crypt($hashed_password . $password,$row['uniqueSalt']);
}

//echo "Password entered by user: ".$hashed_password; echo "<br/>";

$user_db_password = mysqli_query($mysql, "select password from user where email = '".$email."'");
$row = mysqli_fetch_array($user_db_password);
//echo "User's password: ".$row['password']; echo "<br/>";

// query the database to see if there is a record which matches
$query = "select count(*) from user where email = '".$email."' and password = '".$hashed_password."'";
$result = mysqli_query($mysql, $query);
if(!$result) {
    //echo "Cannot run query.";
    exit;
}

$row = mysqli_fetch_row($result);
$count = $row[0];

if ($count > 0) {
    session_start();
    $_SESSION['login'] = 1;
    $_SESSION['email'] = $email;
    $_SESSION['errors'] = "";
    header("location:profile.php");
    //echo "<h1>Login successful!</h1>";
    //echo "<p>Welcome.</p>";
    //echo "<p>This page is only visible when the correct details are provided.</p>";
} else {
    session_start();
    $_SESSION['login'] = '';
    header("location:index.php");
    //echo "<h1>Login unsuccessful!</h1>";
    //echo "<p>The email and password combination entered was not recognized</p>";
}

// CLEAN THE INPUT
function sanitize_input($data)
{
    $data = trim($data);
    $data = stripslashes($data);
    $data = htmlspecialchars($data);
    return $data;
}

?>
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-09T17:58:24+00:00Added an answer on June 9, 2026 at 5:58 pm

    Change this line:

    if ($_SESSION['login'] == 1) {
    

    ..to this:

    if (isset($_SESSION['login']) && $_SESSION['login'] == 1) {
    

    That way, you check if 'login' is set before you access it.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

link Im having trouble converting the html entites into html characters, (&# 8217;) i
Basically, what I'm trying to create is a page of div tags, each has
I know there's a lot of other questions out there that deal with this
I'm trying to decode HTML entries from here NYTimes.com and I cannot figure out
I'm having trouble keeping the paragraph square between the quote marks. In firefox the
I'm trying to create an if statement in PHP that prevents a single post
I need to clean up various Word 'smart' characters in user input, including but
I want to count how many characters a certain string has in PHP, but
I am trying to understand how to use SyndicationItem to display feed which is
I have a jquery bug and I've been looking for hours now, I can't

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.