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 7506979
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 29, 20262026-05-29T22:23:39+00:00 2026-05-29T22:23:39+00:00

Building a simple login page. If the user types in a password and password

  • 0

Building a simple login page. If the user types in a password and password confirmation that don’t match, I want to reset the registration form and print an message. Currently, the message does not print, but carries on through the script. This is what I’ve tried, by way of setting a SESSION variable when the error occurs, and showing this variable upon reload:

registration.php:

<?php
    session_start();
    if (isset($_SESSION['errmsg'])) {
        print($_SESSION['errmsg']);
        unset($_SESSION['errmsg']);
    }
?>

<form name="register" action="register.php" method="post">
    <label>Username</label><input type="text" name="username" maxlength="20" />
    <label>Password</label><input type="password" name="pass" />
    <label>Password Again</label><input type="password" name="pass_confirm" />
    <input type="submit" value="Register" />
</form>

register.php:

<?php
function create_salt() {
    $string = md5(uniqid(rand(), true));
    return substr($string, 0, 3);
}

session_start();
$username = $_POST['username'];
$pass = $_POST['pass'];
$pass_confirm = $_POST['pass_confirm'];

if ($pass != $pass_confirm) {
    $_SESSION['errmsg'] = "Passwords do not match.";
    header('Location: registration.php');
}

if (strlen($username) > 20) {
    header('Location: registration.php');
}

$hash = hash('sha256', $pass);
$salt = create_salt();
$hash = hash('sha256', $salt . $hash);

$conn = mysql_connect('localhost', 'test4', 'test4');
mysql_select_db('test4', $conn);

$username = mysql_real_escape_string($username);

$query = "INSERT INTO users (username, password, salt) VALUES ('$username', '$hash', '$salt');";

mysql_query($query);
mysql_close();
header('Location: index.php');
?>

The important part is the line if ($pass != $pass_confirm) { .... Currently if the passwords do not match this condition is met, but it will carry on through the script rather than reloading via header(Location: registration.php). I am aware that header() cannot be invoked after data has been sent, which is probably causing the problem.

If so, is there a better way to do this in PHP or should I be looking at alternatives?

  • 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-05-29T22:23:40+00:00Added an answer on May 29, 2026 at 10:23 pm

    A die(); or exit; solves the problem.

    header('Location: registration.php');
    die();
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I want to have a login form on a charity website I am building
I'm building a login form that will validate the login against a database, then
I'm building an asp.net page that is designed to display links for a user
I'm getting started with ExtJs. I'm building a very simple login form: Ext.onReady(function ()
I'm building a simple Todo List application where I want to be able to
I am building a simple Django app that will use scribd to display documents.
I'm building a simple accounting app to be used for personal finance. A user
I'm building a simple web based forum application. I want to allow users to
I am building a login page for my website. Right now it is very
I am just building website for our client. It is simple page which lists

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.