Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 8 of 8

Thread: Code help

  1. #1
    New Coder
    Join Date
    Oct 2010
    Posts
    37
    Thanks
    3
    Thanked 0 Times in 0 Posts

    Code help

    i have developed a log in and register system and the registration part is working fine but i am having problems with the log in. for some reason it wont log me in and the pass and usernames are correctly entered as they are in the database.

    Code:
    <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
    <html xmlns="http://www.w3.org/1999/xhtml">
    <head>
    <meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
    <title>Untitled Document</title>
    </head>
    
    <body>
    <?php
            
            // dBase file
            include "dbConfig.php";
    
            if ($_GET["op"] == "login")
      {
      if (!$_POST["username"] || !$_POST["password"])
            {
            die("You need to provide a username and password.");
            }
      
      // Create query
      $q = "SELECT * FROM `dbUsers` "
            ."WHERE `username`='".$_POST["username"]."' "
            ."AND `password`=PASSWORD('".$_POST["password"]."') "
            ."LIMIT 1";
      // Run query
      $r = mysql_query($q);
    
      if ( $obj = @mysql_fetch_object($r) )
            {
            // Login good, create session variables
            $_SESSION["valid_id"] = $obj->id;
            $_SESSION["valid_user"] = $_POST["username"];
            $_SESSION["valid_time"] = time();
    
            // Redirect to member page
            Header("Location: members.php");
            }
      else
            {
            // Login not successful
            die("Sorry, could not log you in. Wrong login information.");
            }
      }
            else
      {
    //If all went right the Web form appears and users can log in
      echo "<form action=\"?op=login\" method=\"POST\">";
      echo "Username: <input name=\"username\" size=\"15\"><br />";
      echo "Password: <input type=\"password\" name=\"password\" size=\"8\"><br />";
      echo "<input type=\"submit\" value=\"Login\">";
      echo "</form>";
      }
            ?>
    
    </body>
    </html>

  2. #2
    Supreme Master coder!
    Join Date
    Jun 2003
    Location
    Cottage Grove, Minnesota
    Posts
    10,107
    Thanks
    10
    Thanked 1,159 Times in 1,150 Posts
    At the very TOP of your script ... above the HTML stuff ... at the top ...


    <?php
    session_start();
    ?>

  3. #3
    New Coder
    Join Date
    Oct 2010
    Posts
    37
    Thanks
    3
    Thanked 0 Times in 0 Posts
    giving me the following error

    Warning: session_start() [function.session-start]: Cannot send session cookie - headers already sent by (output started at /home2/yourclas/public_html/login.php:2) in /home2/yourclas/public_html/login.php on line 2

    Warning: session_start() [function.session-start]: Cannot send session cache limiter - headers already sent (output started at /home2/yourclas/public_html/login.php:2) in /home2/yourclas/public_html/login.php on line 2

    here is the link to the login: yourclassnotes.com/login.php user name: test password: test

  4. #4
    New Coder
    Join Date
    Oct 2010
    Posts
    37
    Thanks
    3
    Thanked 0 Times in 0 Posts
    this is the table


    Name Type Addition
    id int(10) Primary Key, AUTO_INCREMENT
    username varchar(16) Unique
    password char(16)
    email varchar(25)

  5. #5
    Regular Coder
    Join Date
    Oct 2009
    Location
    Florida
    Posts
    156
    Thanks
    16
    Thanked 2 Times in 2 Posts
    Could you post the updated code?
    Make sure you put the ';' after 'session_start()'

  6. #6
    New Coder
    Join Date
    Oct 2010
    Posts
    37
    Thanks
    3
    Thanked 0 Times in 0 Posts
    here is the updated login code
    Code:
    <?php session_start(); ?>
    <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
    <html xmlns="http://www.w3.org/1999/xhtml">
    <head>
    <meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
    <title>Untitled Document</title>
    </head>
    
    <body>
    <?php
    
              // dBase file
            include "dbConfig.php";
    
            if ($_GET["op"] == "login")
      {
      if (!$_POST["username"] || !$_POST["password"])
            {
            die("You need to provide a username and password.");
            }
      
      // Create query
      $q = "SELECT * FROM `dbUsers` "
            ."WHERE `username`='".$_POST["username"]."' "
            ."AND `password`=PASSWORD('".$_POST["password"]."') "
            ."LIMIT 1";
      // Run query
      $r = mysql_query($q);
    
      if ( $obj = @mysql_fetch_object($r) )
            {
            // Login good, create session variables
            $_SESSION["valid_id"] = $obj->id;
            $_SESSION["valid_user"] = $_POST["username"];
            $_SESSION["valid_time"] = time();
    
            // Redirect to member page
            Header("Location: members.php");
            }
      else
            {
            // Login not successful
            die("Sorry, could not log you in. Wrong login information.");
            }
      }
            else
      {
    //If all went right the Web form appears and users can log in
      echo "<form action=\"?op=login\" method=\"POST\">";
      echo "Username: <input name=\"username\" size=\"15\"><br />";
      echo "Password: <input type=\"password\" name=\"password\" size=\"8\"><br />";
      echo "<input type=\"submit\" value=\"Login\">";
      echo "</form>";
      }
            ?>
    
    </body>
    </html>
    i can not find a reason as to why the registration works but not the log in

  7. #7
    Regular Coder
    Join Date
    Oct 2009
    Location
    Florida
    Posts
    156
    Thanks
    16
    Thanked 2 Times in 2 Posts
    Did you create this code yourself, or from another source?


    Also....
    Code:
    // dBase file
            include "dbConfig.php";
    Are you sure shouldn't be:
    Code:
    // dBase file
            include("dbConfig.php");
    Last edited by The reaper; 10-10-2010 at 08:44 PM.

  8. #8
    Supreme Master coder!
    Join Date
    Jun 2003
    Location
    Cottage Grove, Minnesota
    Posts
    10,107
    Thanks
    10
    Thanked 1,159 Times in 1,150 Posts
    Is there a "session_start()" at the top of this script .. "dbConfig.php" ?

    If so, you can't have two of them ... so do this ...
    PHP Code:

    <?php 
    include "dbConfig.php";
    ?>
    <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
    <html xmlns="http://www.w3.org/1999/xhtml">
    <head>
    <meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
    <title>Untitled Document</title>
    </head>

    <body>
    <?php

      
    // blah blah  the rest of your script


 

Posting Permissions

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