SitePoint Sponsor

User Tag List

Results 1 to 12 of 12
  1. #1
    SitePoint Guru
    Join Date
    Feb 2007
    Posts
    731
    Mentioned
    2 Post(s)
    Tagged
    0 Thread(s)

    Updating Header Information?

    Hi,

    I have membership registration code which enables people create a profile. However it creates an error "Cannot modify header information - headers already sent"

    Can anyone advise what the header is or what this means?

    Is it peice of code which records if someone is logged in? If so how do I apply this?

    Code:
        if(!$error) {
            $query = mysql_query("INSERT INTO organisermembers (companyname, password, emailaddress) VALUES ('".$companyname."', '".mysql_real_escape_string(md5($password))."', '".$emailaddress."')");
            if($query) {
                $message = "Hello ".$_POST['companyname'].",\r\n\r\nThanks for registering! We hope you enjoy your stay.\r\n\r\nThanks,\r\nJohn Doe";
                $headers = "From: ".$website['name']." <".$website['emailaddress'].">\r\n";
                mail($_POST['emailaddress'], "Welcome", $message, $headers);
                setcookie("user", mysql_insert_id(), $time);
                setcookie("pass", mysql_real_escape_string(md5($password)), $time);
                header("Location: index.php");
            } else {
                $error = "There was a problem with the registration. Please try again.";
            }
        }
    }

  2. #2
    Hosting Team Leader silver trophybronze trophy
    cpradio's Avatar
    Join Date
    Jun 2002
    Location
    Ohio
    Posts
    5,050
    Mentioned
    152 Post(s)
    Tagged
    0 Thread(s)
    What is above this segment of code? As you likely have either HTML output or an echo statement of some sort that is causing this error
    Be sure to congratulate Patche on earning July's Member of the Month
    Go ahead and blame me, I still won't lose any sleep over it
    My Blog | My Technical Notes

  3. #3
    SitePoint Guru
    Join Date
    Feb 2007
    Posts
    731
    Mentioned
    2 Post(s)
    Tagged
    0 Thread(s)
    Hi,

    This is the complete code. Does the code need to be placed at the top of the page?

    The code has this in it but I dont have a header on the index.php page. Im not sure what a header is to be honest.

    Code:
    header("Location: index.php");

    Code:
    <?php
    
    if(isset($_POST['submit'])){
        $companyname = mysql_real_escape_string(trim($_POST['companyname']));
        $password = trim($_POST['password']);
    	$password1 = mysql_real_escape_string(trim($_POST['password1']));
        $emailaddress = mysql_real_escape_string(trim($_POST['emailaddress']));
        
        if(!isset($companyname) || empty($companyname)) {
            $error = "You need to enter a Company Name.";
        }
    
    
    
        if((!isset($password) || empty($password)) && !$error) {
            $error = "You need to enter a password.";
        }
        if((!isset($password1) || empty($password1)) && !$error) {
            $error = "You need to enter your password twice.";
        }
        if($password != $password1 && !$error) {
            $error = "The passwords you entered did not match.";
        }
    
        
        if((!isset($emailaddress) || empty($emailaddress)) && !$error) {
            $error = "You need to enter an email.";
        }
        if(preg_match("/[a-zA-Z0-9-.+]+@[a-zA-Z0-9-]+.[a-zA-Z]+/", $emailaddress) == 0 && !$error) {
            $error = "The email you entered is not valid.";
        }
        $query = mysql_query("SELECT userid FROM organisermembers WHERE emailaddress = '".$emailaddress."' LIMIT 1");
        if(mysql_num_rows($query) > 0 && !$error) {
            $error = "Sorry, that email is already in use!";
        }
        
        if(!$error) {
            $query = mysql_query("INSERT INTO organisermembers (companyname, password, emailaddress) VALUES ('".$companyname."', '".mysql_real_escape_string(md5($password))."', '".$emailaddress."')");
            if($query) {
                $message = "Hello ".$_POST['companyname'].",\r\n\r\nThanks for registering! We hope you enjoy your stay.\r\n\r\nThanks,\r\nJohn Doe";
                $headers = "From: ".$website['name']." <".$website['emailaddress'].">\r\n";
                mail($_POST['emailaddress'], "Welcome", $message, $headers);
                setcookie("user", mysql_insert_id(), $time);
                setcookie("pass", mysql_real_escape_string(md5($password)), $time);
                header("Location: index.php");
            } else {
                $error = "There was a problem with the registration. Please try again.";
            }
        }
    }
    
    ?>

  4. #4
    Hosting Team Leader silver trophybronze trophy
    cpradio's Avatar
    Join Date
    Jun 2002
    Location
    Ohio
    Posts
    5,050
    Mentioned
    152 Post(s)
    Tagged
    0 Thread(s)
    Quote Originally Posted by justlukeyou View Post
    This is the complete code. Does the code need to be placed at the top of the page?
    Yes. Anytime you use setcookie() or header(), your code needs to be at the top of the page.
    Be sure to congratulate Patche on earning July's Member of the Month
    Go ahead and blame me, I still won't lose any sleep over it
    My Blog | My Technical Notes

  5. #5
    SitePoint Guru
    Join Date
    Feb 2007
    Posts
    731
    Mentioned
    2 Post(s)
    Tagged
    0 Thread(s)
    Quote Originally Posted by cpradio View Post
    Yes. Anytime you use setcookie() or header(), your code needs to be at the top of the page.
    Thanks, so what is a header. Is there something I should have on the index page?

  6. #6
    Hosting Team Leader silver trophybronze trophy
    cpradio's Avatar
    Join Date
    Jun 2002
    Location
    Ohio
    Posts
    5,050
    Mentioned
    152 Post(s)
    Tagged
    0 Thread(s)
    A header is a command to the browser. You can use Location to tell the browser, redirect to the page/url I give you, you can use CONTENT-ENCODING to set a specific encoding to the page, etc. setcookie uses a header to tell the browser to write a cookie on the user's computer. So since these are instructions to the browser, that is why they need to be at the top of the page. By the time you start writing output (HTML or TEXT) the browser is already done executing any instructions and is only focused on writing your content to the screen.
    Be sure to congratulate Patche on earning July's Member of the Month
    Go ahead and blame me, I still won't lose any sleep over it
    My Blog | My Technical Notes

  7. #7
    SitePoint Guru
    Join Date
    Feb 2007
    Posts
    731
    Mentioned
    2 Post(s)
    Tagged
    0 Thread(s)
    Thanks, alot clearer now. So is there a header that should be used to confirm whether someone is logged into a database as a member or does the header Location: index.php redirect the user to the homepage once someone has logged in?

    Do I need to do these two points?

  8. #8
    Hosting Team Leader silver trophybronze trophy
    cpradio's Avatar
    Join Date
    Jun 2002
    Location
    Ohio
    Posts
    5,050
    Mentioned
    152 Post(s)
    Tagged
    0 Thread(s)
    header("Location: index.php") is redirecting the user to the home page once someone logs in. It is technically your decision on whether you want to redirect them or not, or leave them on the page the registration page (or redirect to a confirmation page for that matter).

    A header does not and cannot confirm whether someone is logged in. You have to code that behavior. To do that, you would need to read your cookies using the $_COOKIE['name of your cookie'] super-global variable and then query those values against your database to verify the user is valid.
    Be sure to congratulate Patche on earning July's Member of the Month
    Go ahead and blame me, I still won't lose any sleep over it
    My Blog | My Technical Notes

  9. #9
    SitePoint Guru
    Join Date
    Feb 2007
    Posts
    731
    Mentioned
    2 Post(s)
    Tagged
    0 Thread(s)
    I have the following page at the top of the page. Is there something I am missing to read if someone is logged in, is there anything which could be reading if someone is logged in. Is there code that reads if someone is logged in that I need to add to the homepage?



    Code:
    <login code
    <?php
    
    if(isset($_POST['submit'])){
        $companyname = mysql_real_escape_string(trim($_POST['companyname']));
        $password = trim($_POST['password']);
    	$password1 = mysql_real_escape_string(trim($_POST['password1']));
        $emailaddress = mysql_real_escape_string(trim($_POST['emailaddress']));
        
        if(!isset($companyname) || empty($companyname)) {
            $error = "You need to enter a Company Name.";
        }
        if(preg_match("/[a-zA-Z0-9-.+]+@[a-zA-Z0-9-]+.[a-zA-Z]+/", $companyname) == 0 && !$error) {
            $error = "The Company Name you entered is not valid.";
        }
    
    
        if((!isset($password) || empty($password)) && !$error) {
            $error = "You need to enter a password.";
        }
        if((!isset($password1) || empty($password1)) && !$error) {
            $error = "You need to enter your password twice.";
        }
        if($password != $password1 && !$error) {
            $error = "The passwords you entered did not match.";
        }
    
        
        if((!isset($emailaddress) || empty($emailaddress)) && !$error) {
            $error = "You need to enter an email.";
        }
        if(preg_match("/[a-zA-Z0-9-.+]+@[a-zA-Z0-9-]+.[a-zA-Z]+/", $emailaddress) == 0 && !$error) {
            $error = "The email you entered is not valid.";
        }
        $query = mysql_query("SELECT userid FROM organisermembers WHERE emailaddress = '".$emailaddress."' LIMIT 1");
        if(mysql_num_rows($query) > 0 && !$error) {
            $error = "Sorry, that email is already in use!";
        }
        
        if(!$error) {
            $query = mysql_query("INSERT INTO organisermembers (companyname, password, emailaddress) VALUES ('".$companyname."', '".mysql_real_escape_string(md5($password))."', '".$emailaddress."')");
            if($query) {
                $message = "Hello ".$_POST['companyname'].",\r\n\r\nThanks for registering! We hope you enjoy your stay.\r\n\r\nThanks,\r\nJohn Doe";
                $headers = "From: ".$website['name']." <".$website['emailaddress'].">\r\n";
                mail($_POST['emailaddress'], "Welcome", $message, $headers);
                setcookie("user", mysql_insert_id(), $time);
                setcookie("pass", mysql_real_escape_string(md5($password)), $time);
                header("Location: /index.php");
            } else {
                $error = "There was a problem with the registration. Please try again.";
            }
        }
    }
    
    ?>

  10. #10
    Hosting Team Leader silver trophybronze trophy
    cpradio's Avatar
    Join Date
    Jun 2002
    Location
    Ohio
    Posts
    5,050
    Mentioned
    152 Post(s)
    Tagged
    0 Thread(s)
    That looks more like a registration setup than a login page.

    A login page would look more like the following:
    PHP Code:
    <?php

    if(isset($_POST['submit'])){
        
    $password trim($_POST['password']);
        
    $emailaddress mysql_real_escape_string(trim($_POST['emailaddress']));

        if((!isset(
    $password) || empty($password)) && !$error) {
            
    $error "You need to enter a password.";
        }

        if((!isset(
    $emailaddress) || empty($emailaddress)) && !$error) {
            
    $error "You need to enter an email.";
        }
        if(
    preg_match("/[a-zA-Z0-9-.+]+@[a-zA-Z0-9-]+.[a-zA-Z]+/"$emailaddress) == && !$error) {
            
    $error "The email you entered is not valid.";
        }

        if(!
    $error) {
            
    $query mysql_query("SELECT id FROM organisermembers WHERE emailaddress='$emailaddress' AND password='".mysql_real_escape_string(md5($password))."'");
            if(
    $query && mysql_num_rows($query) == 1) {
                
    $row mysql_fetch_assoc($query);
                
    setcookie("user"$row['id'], $time);
                
    setcookie("pass"mysql_real_escape_string(md5($password)), $time);
            } else {
                
    $error "There was a problem with the login. Please try again.";
            }
        }
    }

    ?>
    Be sure to congratulate Patche on earning July's Member of the Month
    Go ahead and blame me, I still won't lose any sleep over it
    My Blog | My Technical Notes

  11. #11
    SitePoint Guru
    Join Date
    Feb 2007
    Posts
    731
    Mentioned
    2 Post(s)
    Tagged
    0 Thread(s)
    Yes your right sorry, that is a registration setup. What I would like to do is to add a email authentication script once someone registers for the site.

    Is the code that reads if someone is logged in on everypage or is it just on the homepage?

  12. #12
    Hosting Team Leader silver trophybronze trophy
    cpradio's Avatar
    Join Date
    Jun 2002
    Location
    Ohio
    Posts
    5,050
    Mentioned
    152 Post(s)
    Tagged
    0 Thread(s)
    Quote Originally Posted by justlukeyou View Post
    Yes your right sorry, that is a registration setup. What I would like to do is to add a email authentication script once someone registers for the site.
    Check example 1 on http://us3.php.net/manual/en/function.filter-var.php

    Quote Originally Posted by justlukeyou View Post
    Is the code that reads if someone is logged in on everypage or is it just on the homepage?
    Most websites have either a login page or a login form on each page, if you have the latter, you need the login code on all of your pages.
    Be sure to congratulate Patche on earning July's Member of the Month
    Go ahead and blame me, I still won't lose any sleep over it
    My Blog | My Technical Notes


Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •