Board index   FAQ   Search  
Register  Login
Board index php forum :: Database mySQL & php coding

SESSIONS?

Codes here !

Moderators: macek, egami, gesf

SESSIONS?

Postby Ken Masters » Sat Mar 15, 2003 2:21 pm

Hi guys,

I'm new to this forum. I've been learning PHP over the last few months. It's a great language and I much prefer over it over ASP. I have question about Sessions. I found a tutorial at phpfreaks.com. It's a login application. I get this error when running the checkuser.php,

Warning: Cannot send session cookie - headers already
sent by (output started at
/home/aphexweb/www/www/login/checkuser.php:7) in
/home/aphexweb/www/www/login/checkuser.php on line 67

Warning: Cannot send session cache limiter - headers
already sent (output started at
/home/aphexweb/www/www/login/checkuser.php:7) in
/home/aphexweb/www/www/login/checkuser.php on line 67

Warning: Cannot add header information - headers already
sent by (output started at
/home/aphexweb/www/www/login/checkuser.php:7) in
/home/aphexweb/www/www/login/checkuser.php on line 78

I have no idea how these warnings come about because the session started on checkuser.php.

Anyways I'll attach the three files associated with this application up to these warnings.

Your help is very much appreciated. I plan to be a very active member of this forum and look forward to learning and maybe teaching PHP. :)


This is the initial php script the where the form variables are sent to:

Code: Select all

<?php

include 'db_connect.php';

//defining post fields into simple variables

$first_name = $_POST['first_name'];
$last_name = $_POST['last_name'];
$email_address = $_POST['email_address'];
$username = $_POST['username'];
$info = $_POST['info']; 

$first_name = stripslashes($first_name);
$last_name = stripslashes($last_name);
$email_address = stripslashes($email_address);
$username = stripslashes($username);
$info = stripslashes($info);


//check for errors

if((!$first_name) || (!$last_name) || (!$email_address) || (!$username)){
   
   echo '<font color=\"#ff0000\">You did not submit the following required information!</font><br>';
    if(!$first_name){
        echo '<font color=\"#ff0000\">First Name is a required field. Please enter it below.</font><br>';
    }
    if(!$last_name){
        echo '<font color=\"#ff0000\">Last Name is a required field. Please enter it below.</font><br>';
    }
    if(!$email_address){
        echo '<font color=\"#ff0000\">Email Address is a required field. Please enter it below.</font><br>';
    }
    if(!$username){
        echo '<font color=\"#ff0000\">Username is a required field. Please enter it below.</font><br>';
    }
    include 'join_form.html'; 
     
    exit();
}
     
//check if username and email address exist in database

 $sql_email_check = mysql_query("SELECT email_address FROM users 
            WHERE email_address='$email_address'");
 $sql_username_check = mysql_query("SELECT username FROM users 
            WHERE username='$username'");

 $email_check = mysql_num_rows($sql_email_check);
 $username_check = mysql_num_rows($sql_username_check);

 if(($email_check > 0) || ($username_check > 0))  {
   
   echo '<font color=\"#ff0000\">Please fix the following errors: </font><br><br>';
 
    if($email_check > 0)  {
       
      echo '<font color=\"#ff0000\">Email address already exists.  Please choose another email address.</font><br>';
           unset($email_address);
                   }
   
  if($username_check > 0)  {
       
   echo '<font color=\"#ff0000\">Username already exists.  Please choose another username.</font><br>';
        unset($username);
                }
   
  include 'join_form.html';
     
    exit();
 
 }

//generate random password and encrypt it
 

function makeRandomPassword() {
  $salt = "abchefghjkmnpqrstuvwxyz0123456789";
  srand((double)microtime()*1000000); 
      $i = 0;
      while ($i <= 7) {
            $num = rand() % 33;
            $tmp = substr($salt, $num, 1);
            $pass = $pass . $tmp;
            $i++;
      }
      return $pass;
}

$random_password = makeRandomPassword();

$db_password = md5($random_password);

//enter info into the database
 
$info2 = htmlspecialchars($info);
$sql = mysql_query("INSERT INTO users (first_name, last_name,
        email_address, username, password, info, signup_date)
        VALUES('$first_name', '$last_name', '$email_address',
        '$username', '$db_password', '$info2', now())") 
        or die (mysql_error());

if(!$sql)  {
   
   echo '<font color=\"#ff0000\">There has been an error creating your account. Please contact the webmaster.</font><br>';

   } else {
   
    $userid = mysql_insert_id();
   
//send email
 
    $subject = "Account";
    $message = "Dear $first_name $last_name,
    Thank you for registering at our website, http://www.something.com
     
    You are two steps away from logging in.
     
    To activate your account,
    please click here: http://www.something.com/login/activate.php?id=$userid&code=$db_password
     
    Once you activate your membership, you will be able to login
    with the following information:
    Username: $username
    Password: $random_password
     
    Thanks!
    The Webmaster
     
    This is an automated response, please do not reply!";
     
    mail("$email_address", "$subject", "$message");
    echo '<font color=\"#0000ff\">Your membership information has been mailed to your email address!
    Please check it and follow the directions!</font><br>';
}

?>
 


The activate script:

Code: Select all

<?php

include 'db_connect.php';

$userid = $_REQUEST['id'];
$code = $_REQUEST['code'];

$sql = mysql_query("UPDATE users SET activated='1' WHERE userid='$userid' AND password='$code'");

$sql_doublecheck = mysql_query("SELECT * FROM users WHERE userid='$userid' AND password='$code' AND activated='1'");
$doublecheck = mysql_num_rows($sql_doublecheck);

if($doublecheck == 0){
    echo '<font color=\"#ff0000\">Your account could not be activated!</font><br>';
} elseif ($doublecheck > 0) {
    echo '<font color=\"#0000ff\">Your account has been activated! You may login below!</font><br>';
    include 'login_form.html';
}

?>



The checkuser.php:

Code: Select all

<?php

session_start();  //start Session

include 'db_connect.php';
 
//convert to simple variables
$username = $_POST['username'];
$password = $_POST['password'];

if((!$username) || (!$password)){
    echo '<font color=\"#ff0000\">Please enter ALL of the information!</font><br>\n';
    include 'login_form.html';
    exit();
}

//convert password to md5 hash

$password = md5($password);

//check if the user info validates the database
 
$sql = mysql_query("SELECT * FROM users WHERE username='$username' AND password='$password' AND activated='1'");
$login_check = mysql_num_rows($sql);

if($login_check > 0){
    while($row = mysql_fetch_array($sql)){
    foreach( $row AS $key => $val ){
        $$key = stripslashes( $val );
    }
        //register some session variables
        session_register('first_name');
        $_SESSION['first_name'] = $first_name;
        session_register('last_name');
        $_SESSION['last_name'] = $last_name;
        session_register('email_address');
        $_SESSION['email_address'] = $email_address;
        session_register('special_user');
        $_SESSION['user_level'] = $user_level;
         
        mysql_query("UPDATE users SET last_login=now() WHERE userid='$userid'");
         
        header("Location: login_success.php");
    }
} else {
   
    echo '<font color=\"#ff0000\">You could not be logged in! Either the username and password do not match or you have not validated your membership!<br>
    Please try again!</font><br>\n'; 
    include 'login_form.html';

       }

?>



The login_success.php:

Code: Select all

<?php

session_start();

echo '<font color=\"#0000ff\">Welcome ". $_SESSION['first_name'] ." ". $_SESSION['last_name'] ."! You have logged in</font><br>\n';

echo '<font color=\"#0000ff\">Your user level is ". $_SESSION['user_level'].</font><br>\n';

echo '<font color=\"#0000ff\"><a href=logout.php>Logout</a></font><br>\n';

?>

Ken Masters
New php-forum User
New php-forum User
 
Posts: 4
Joined: Sat Mar 15, 2003 1:43 pm
Location: Vancouver, B.C.

Postby Romantik » Mon Mar 17, 2003 9:19 am

headers already sent!!!

read http://www.php.net/headers

nothing print before sessions!
Romantik
New php-forum User
New php-forum User
 
Posts: 67
Joined: Mon Feb 24, 2003 2:55 am
Location: ../Ukraine/Kherson

Postby Ken Masters » Mon Mar 17, 2003 12:58 pm

Romantik wrote:headers already sent!!!

read http://www.php.net/headers


I know the headers have already been sent, but I don't where in the script.

I'm a newbie so I'm not sure what I should be looking for. :(

nothing print before sessions!
I don't understand what you mean by that. :?:
Ken Masters
New php-forum User
New php-forum User
 
Posts: 4
Joined: Sat Mar 15, 2003 1:43 pm
Location: Vancouver, B.C.

Postby Romantik » Tue Mar 18, 2003 1:20 am

"View source code" do it in your browser and you will see a spases or text or etc.
Romantik
New php-forum User
New php-forum User
 
Posts: 67
Joined: Mon Feb 24, 2003 2:55 am
Location: ../Ukraine/Kherson

Postby Redcircle » Tue Mar 18, 2003 9:38 am

Quote:
nothing print before sessions!
I don't understand what you mean by that.


basically nothing can be output to browser
User avatar
Redcircle
Moderator
Moderator
 
Posts: 830
Joined: Tue Jan 21, 2003 10:42 pm
Location: Michigan USA

Postby Ken Masters » Tue Mar 18, 2003 1:34 pm

Guys,

I'm new to PHP. Do you think you could elaborate? Pretend your teaching a 5 year old. What do I need to do to get checkuser.php to work?

It's a school assignment that's due on the 23rd. :(
Ken Masters
New php-forum User
New php-forum User
 
Posts: 4
Joined: Sat Mar 15, 2003 1:43 pm
Location: Vancouver, B.C.

Postby Ihoss » Tue Apr 01, 2003 3:24 am

question: What are sessions :?:
Ihoss
New php-forum User
New php-forum User
 
Posts: 17
Joined: Sun Mar 30, 2003 11:41 pm

Postby Redcircle » Wed Apr 02, 2003 5:11 pm

Ihoss wrote:question: What are sessions :?:


http://www.php.net/manual/en/ref.session.php
User avatar
Redcircle
Moderator
Moderator
 
Posts: 830
Joined: Tue Jan 21, 2003 10:42 pm
Location: Michigan USA

Postby Ihoss » Mon Apr 07, 2003 8:03 am

i tried it but it doesent give an explenation for a newbie (ie idiot)
please help :(
Ihoss
New php-forum User
New php-forum User
 
Posts: 17
Joined: Sun Mar 30, 2003 11:41 pm


Return to mySQL & php coding

Who is online

Users browsing this forum: Google [Bot] and 1 guest

Sponsored by Sitebuilder Web hosting and Traduzioni Italiano Rumeno and antispam for cPanel.

cron