Hello. I have a two problems with my Regsystem.
1st. Warning: Cannot modify header information - headers already sent by (output started at C:\xampp\htdocs\mycms\reg.php:229) in C:\xampp\htdocs\mycms\reg.php on line 266

2nd: When i Register a user - system message "Username already exist" not "Registration succesfull"..

this is the code of register.php

<?php 
require 'core/init.php';
$general->logged_in_protect();

if (isset($_POST['submit'])) {

    if(empty($_POST['username']) || empty($_POST['password']) || empty($_POST['email'])){

        $errors[] = 'All fields are required.';

    }else{

        if ($users->user_exists($_POST['username']) === true) {
            $errors[] = 'That username already exists'; // ALWAYS TYPED THIS
        }
        if(!ctype_alnum($_POST['username'])){
            $errors[] = 'Please enter a username with only alphabets and numbers';  
        }
        if (strlen($_POST['password']) <6){
            $errors[] = 'Your password must be atleast 6 characters';
        } else if (strlen($_POST['password']) >18){
            $errors[] = 'Your password cannot be more than 18 characters long';
        }
        if (filter_var($_POST['email'], FILTER_VALIDATE_EMAIL) === false) {
            $errors[] = 'Please enter a valid email address';
        }else if ($users->email_exists($_POST['email']) === true) {
            $errors[] = 'That email already exists.';
        }
    }

    if(empty($errors) === true){

        $username   = htmlentities($_POST['username']);
        $password   = $_POST['password'];
        $email      = htmlentities($_POST['email']);

        $users->register($username, $password, $email);
        header('Location: reg.php?success');
        exit();
    }
}

?>

        <?php
        if (isset($_GET['success']) && empty($_GET['success'])) {
          echo 'Thank you for registering. Please check your email.';
        }
        ?>

Recommended Answers

All 7 Replies

Can you show the code of method user_exists()?

    public function user_exists($username) {

        $query = $this->db->prepare("SELECT COUNT(`id`) FROM `users` WHERE `username`= ?");
        $query->bindValue(1, $username);

        try{

            $query->execute();
            $rows = $query->fetchColumn();

            if($rows == 1){
                return true;
            }else{
                return false;
            }

        } catch (PDOException $e){
            die($e->getMessage());
        }

    }

??????

Read the manual.

Remember that header() must be called before any actual output is sent, either by normal HTML tags, blank lines in a file, or from PHP. It is a very common error to read code with include, or require, functions, or another file access function, and have spaces or empty lines that are output before header() is called. The same problem exists when using a single PHP/HTML file.

Source.

all right, but why after registration in the database are two identical account?

What exactly trubles you with the message "Cannot modify header information" ? The message is complete you have an outpout in line 229 .... What it should say more ?

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.