473,403 Members | 2,270 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,403 software developers and data experts.

Help for Login

9
Hi,
i can register and login without fail.
However i notice that my inputs are not record into the database.
I do not know the reason.
Can someone guide me into login and register.
Actually im quite confuse with the codings

login.php
Expand|Select|Wrap|Line Numbers
  1. <?php
  2. session_start ();
  3.             $HOST = 'localhost';
  4.             $USERNAME = 'root';
  5.             $PASSWORD = '';
  6.             $DB = 'c203';
  7.             $username = $_POST['username'];
  8.             $password = $_POST['password'];
  9.  
  10.         $link = mysqli_connect ($HOST,$USERNAME,$PASSWORD,$DB)or die(mysqli_connect_error());
  11.         $sql = "SELECT username,password FROM login WHERE username='".$USERNAME."' AND password = SHA1('".$PASSWORD."')";
  12.         $result = mysqli_query($link,$sql) or die (mysqli_error($link));
  13.  
  14.         if (mysqli_num_rows($result) == 1) {
  15.         $row = mysqli_fetch_array($result);
  16.         $_SESSION['username'] = $row['username'];
  17.         $_SESSION['password'] = $row['password'];
  18.         $msg = '<p><i>You are logged in as '.$SESSION['username'].'<br/><a href="index.php">Home</p>';
  19.  
  20. }        else {
  21.         $msg = '<p class ="error"> Sorry, you must enter a valid username and password to log in. <a href ="index.php">Back</a></p>';
  22. }
  23.  
  24. ?>
  25.  
  26. <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
  27.   "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
  28. <html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en" lang="en">
  29. <head>
  30. <meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
  31. <title>StarGazer - Login</title>
  32. <link rel="stylesheet" type="text/css" href="style.css" />
  33. </head>
  34. <body>
  35. <h3>StarGazer - Login</h3>
  36. <?php
  37.  
  38. echo $msg
  39.  
  40. ?>
  41. </body>
  42. </html>

loginPage.php
Expand|Select|Wrap|Line Numbers
  1. <?php
  2. session_start();
  3. if(isset($_SESSION['user_id'])) {
  4.     echo"<p>You are already logged in ...<a href=\"index.php\">Back</a></p>";
  5.  
  6.     exit;
  7. }
  8. ?>
  9. <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
  10.   "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
  11. <html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en" lang="en">
  12. <head>
  13.   <meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
  14.   <title>StarGazer!</title>
  15.   <link rel="stylesheet" type="text/css" href="style.css" />
  16. </head>
  17. <body>
  18.   <h3>StarGazer- Login</h3>
  19.   <form method="post" action="login.php">
  20.       <fieldset>
  21.           <legend>Login</legend>
  22.         <table>
  23.             <tr>
  24.                 <td><label for="username">Username:</label></td>
  25.                 <td><input type="text" id="username" name="userName"/></td>
  26.             </tr>
  27.             <tr>
  28.                 <td><label for="password">Password:</label></td>
  29.                 <td><input type="password" id="password" name="password"/></td>
  30.             </tr>
  31.         </table>
  32.       </fieldset>
  33.     <input type="submit" value="Login" name="submit"/>
  34.   </form>
  35.     </body>
  36. </html>
  37.  
register.php
Expand|Select|Wrap|Line Numbers
  1. <?php
  2. if(isset($_POST)){
  3.     //retrieve form data
  4.     $name = $_POST['name'];
  5.     $gender = $_POST['gender'];
  6.     $birthdate = $_POST['birthdate'];
  7.     $username = $_POST['username'];
  8.     $password1 = $_POST['password1'];
  9.  
  10.     //connect to database        
  11.     $HOST = 'localhost';
  12.     $USERNAME = 'root';
  13.     $PASSWORD = '';
  14.     $DB = 'c203';
  15.  
  16.     $link = mysqli_connect($HOST,$USERNAME,$PASSWORD,$DB);
  17.     $query = "INSERT INTO register(name,gender,birthdate,username,password) VALUES ('".$name."','".$gender."','".$birthdate."','".$username."',SHA1('".$password1."'))";
  18.     $status = mysqli_query($link,$query) or die(mysqli_error($link));
  19.  
  20.     //insert new record
  21.     if($status){
  22.         $message = '<p>Your new account has been successfully created. You are now ready to <a href="index.php">Login</a>.</p>';
  23.         $message .= '<p><a href="index.php">Home</a>';
  24.     }
  25.     mysqli_close($link);
  26. }else {//form not posted
  27.     $message = '<p class="error">You must enter all of the sign-up data.<a href="register.html">Back</a></p>';
  28. }
  29. ?>
  30. <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
  31.   "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
  32. <html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en" lang="en">
  33. <head>
  34. <meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
  35. <title>StarGazer - Register</title>
  36. <link rel="stylesheet" type="text/css" href="style.css" />
  37. </head>
  38. <body>
  39. <h3>StarGazer - Register</h3>
  40. <?php
  41. echo $message;
  42. ?>
  43. </body>
  44. </html>
Jan 12 '10 #1
1 1933
Atli
5,058 Expert 4TB
Hey.

I assume you mean that your database is filled with empty rows?

There are two things in your registration script that could/would cause this:
  • You can not verify that a form has been submitted by checking if the $_POST array is set. It is always set, regardless of whether it has any data.
    Expand|Select|Wrap|Line Numbers
    1. // This is ALWAYS true. Do not do this
    2. // to check if a form has been posted
    3. if(isset($_POST)) { //... }
    4.  
    5. // Instead, check the actual fields.
    6. if(issset($_POST['field1'], $_POST['field2'] /* etc.. */)) { ... }
    7.  
    8. // ... And yes, you should check ALL fields that
    9. // should be present.
    10.  
  • Which brings me to me second point: Validating the data.
    You should always make sure your data is in fact what it is supposed to be. As it is, your script doesn't so much as check whether the fields have any data. Which means - coupled with what I discussed earlier - your script inserts a row with empty data every time somebody opens your registration script to see the form.

    Never trust the user to insert valid data. Always assume the user is trying to manipulate your system to do something harmful to your site, and code accordingly.
    Look up the phrase "SQL Injection". You could start by reading the chapter on it in the PHP manual.
Jan 12 '10 #2

Sign in to post your reply or Sign up for a free account.

Similar topics

3
by: neilphan | last post by:
Hi Guys, Please HELP! I'm new to PHP and would like to get your professional help! I"m writing simple and small login app using php session variable. I have 3 php scripts. The first is just a...
0
by: Yvonne | last post by:
Hi! I have a problem and wonder if anyone can help me with that. I am trying to make a community in php/mysql to work but it don't. I hope you can help me even if the community is in swedish. ...
3
by: Nicola Marchiori | last post by:
Hi I need some help. I am developing a Web App in one PC running IIS. I am trying to connect to to MSDE on another PC, but it's not working. This is the message i got Login failed for user...
1
by: Richard Clark | last post by:
I have an ASP 2.0 secured website that works fine. My problem is that I need to run this "mini site" inside a frame. We have a domain hosted website and I'm redirecting the frame contents to...
0
by: Richard Clark | last post by:
I have an ASP 2.0 secured website that works fine. My problem is that I need to run this "mini site" inside a frame. We have a domain hosted website and I'm redirecting the frame contents to...
23
by: casper christensen | last post by:
Hi I run a directory, where programs are listed based on the number of clicks they have recieved. The program with most clicks are placed on top and so on. Now I would like people to be apple to...
6
by: AppleBag | last post by:
I'm having the worst time trying to login to myspace through code. Can someone tell me how to do this? Please try it yourself before replying, only because I have asked this a couple of times in...
5
by: Mai Le | last post by:
Hello Experts. Coul you please help me to fix my program. I created a login from with 2 levels. Admin an User. If Admin login will open A form and If User login will open B form I had table...
3
by: satishknight | last post by:
Hi, Can some one tell me how to change the validation sequence for the code pasted below, actually what I want it when any one enters the wrong login information (already registered users) then it...
13
by: Apostle | last post by:
Hi all, after thinking for sometimes, I thought it will be great opportunity to learn if I will start from scratch and build my own register/login system. Here is the thread that I will be posting...
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.