473,385 Members | 1,445 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,385 software developers and data experts.

login php

i would like to create php login form but the problem is the form doesnt do anything
Expand|Select|Wrap|Line Numbers
  1. <?php require_once("nocache.php"); ?>
  2.  
  3. <?xml version="1.0" encoding="UTF-8"?>
  4. <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN"
  5.   "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd"> 
  6. <html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en" lang="en">
  7. <head>
  8.     <title>login.php</title>
  9. <?php
  10. if(isset($_POST['Submit']))
  11.     if (empty($id) || empty($pword)){
  12.         header("location: login.php");
  13.         }else{
  14.             $conn = mysql_connect("localhost", "TWAassign2", "student");
  15.             if(!$conn){
  16.                 die('Conection could not be opened: ' . mysql_error());
  17.             }
  18.             $db_selected = mysql_select_db('acadcredit', $conn);
  19.             $sql = "SELECT staffID, accessLevel FROM staff WHERE id = '$id' AND password = '$pword'";
  20.             $rs = mysql_query($sql, $conn);
  21.             if (mysql_num_rows($rs)> 0 ) {
  22.                 session_start();
  23.                 $_SESSION["who"] = $id;
  24.                 $_SESSION["level"] = mysql_result($rs,0,"level");
  25.                 header("location: selectView.php");
  26.             }else{
  27.                 header("location: login.php");
  28.             }
  29.         }
  30. ?>
  31.  
  32. </head>
  33. <body>
  34. <form method="post" action="login.php">
  35. <h1> Welcome to Login Page </h1>
  36. <h2> Please enter Your Staff ID and password- Thanks</h2>
  37.     <p>Staff ID  <input type="text" name="ID"  /><br/>
  38.     Password <input type="password" name="pass" /></p> 
  39. <p> 
  40. <?php
  41.     if(isset($_POST['ID'])){
  42.         $id = $_POST["ID"];
  43.         $pword = $_POST["pass"];
  44.     }
  45. ?>
  46.  
  47.     <input type="submit" value="login" />
  48.     <input type="reset"  value="Reset" /></p>
  49. </form>
  50. </body>
  51. </html>
  52.  
why isnt working
thanks
May 18 '09 #1
8 2266
prabirchoudhury
162 100+
Expand|Select|Wrap|Line Numbers
  1.  
  2. <?php session_start();
  3. // have to initialised session_start(); top of the page 
  4. ?>
  5. <?xml version="1.0" encoding="UTF-8"?>
  6. <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN"
  7. "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd"> 
  8. <html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en" lang="en">
  9. <head>
  10. <title>login.php</title>
  11. <?php
  12. if(isset($_POST['Submit'])){
  13.  
  14.     $id= $_POST['ID'];
  15.     $pword= $_POST['pass'];
  16.  
  17. }
  18. if (empty($id) || empty($pword)){
  19. header("location: login.php");
  20. }else{
  21. $conn = mysql_connect("localhost", "TWAassign2", "student");
  22. if(!$conn){
  23. die('Conection could not be opened: ' . mysql_error());
  24. }
  25. $db_selected = mysql_select_db('acadcredit', $conn);
  26. $sql = "SELECT staffID, accessLevel FROM staff WHERE id = '$id' AND password = '$pword'";
  27. $rs = mysql_query($sql, $conn);
  28. if (mysql_num_rows($rs)> 0 ) {
  29. session_start();
  30. $_SESSION["who"] = $id;
  31. $_SESSION["level"] = mysql_result($rs,0,"level");
  32. header("location: selectView.php");
  33. }else{
  34. header("location: login.php");
  35. }
  36. }
  37. ?>
  38.  
  39. </head>
  40. <body>
  41. <form method="post" action="login.php">
  42. <h1> Welcome to Login Page </h1>
  43. <h2> Please enter Your Staff ID and password- Thanks</h2>
  44. <p>Staff ID <input type="text" name="ID" /><br/>
  45. Password <input type="password" name="pass" /></p> 
  46. <p> 
  47.     <!-- this pice of code goes top of the page then tou check $_POST['submit'] when you submit the page then passing $id and $pword would be
  48.     initialised,
  49.  
  50.      -->
  51. <?php
  52. /*
  53. if(isset($_POST['ID'])){
  54. $id = $_POST["ID"];
  55. $pword = $_POST["pass"];
  56. }*/
  57. ?>
  58.  
  59. <input type="submit" value="login" />
  60. <input type="reset" value="Reset" /></p>
  61. </form>
  62. </body>
  63. </html>
  64.  
  65.  
  66.  
May 18 '09 #2
i did what you said but still doesnt do anything not giving error or goes to next page and doesnt submit to itself so whats wrong with it
May 18 '09 #3
prabirchoudhury
162 100+
try this and if any error come mention that
Expand|Select|Wrap|Line Numbers
  1. <?php session_start();
  2. // have to initialised session_start(); top of the page 
  3. ?>
  4.  
  5. <html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en" lang="en">
  6. <head>
  7. <title>login.php</title>
  8. </head>
  9. <body >
  10.     <br><br>
  11.  
  12.  
  13.  <?
  14.  
  15.  if(isset($_POST['submit'])){
  16.  
  17.     $id= $_POST['ID'];
  18.     $pword= $_POST['pass'];
  19.     /echo "password".$pword;
  20.  //echo "id".$id;
  21. }
  22.  if (empty($id) || empty($pword)){
  23. header("location: login.php");
  24. }else{
  25. $conn = mysql_connect("localhost", "TWAassign2", "student");
  26. if(!$conn){
  27. die('Conection could not be opened: ' . mysql_error());
  28. }
  29. $db_selected = mysql_select_db('acadcredit', $conn);
  30. $sql = "SELECT staffID, accessLevel FROM staff WHERE id = '$id' AND password = '$pword'";
  31. $rs = mysql_query($sql, $conn);
  32. if (mysql_num_rows($rs)> 0 ) {
  33.  
  34. $_SESSION["who"] = $id;
  35. $_SESSION["level"] = mysql_result($rs,0,"level");
  36. header("location: selectView.php");
  37. }else{
  38. header("location: login.php");
  39. }
  40. }
  41.  ?>
  42.   <!--=============================================================================-->
  43.  <form method="post" action="<?php echo $_SERVER['PHP_SELF']; ?>">
  44. <h1> Welcome to Login Page </h1>
  45. <h2> Please enter Your Staff ID and password- Thanks</h2>
  46. <p>Staff ID <input type="text" name="ID" /><br/>
  47. Password <input type="password" name="pass" /></p> 
  48.  
  49. <input type="submit" value="login" />
  50. <input type="reset" value="Reset" /></p>
  51. </form>
  52.  
  53.  
  54.  
  55. </body>
  56. </html>
  57.  
  58.  
  59.  
  60.  
May 18 '09 #4
the error was no input satisfied and then page not found why? and i have this at the top of page0 ) { $_SESSION["who"] = $id; $_SESSION["level"] = mysql_result($rs,0,"level"); header("location: selectView.php"); }else{ header("location: login.php"); } } ?> please help thanks
May 18 '09 #5
i realy need help to create the login page as soon as possible please help
thanks
May 19 '09 #6
Ciary
247 Expert 100+
creating a login is really simple:

Expand|Select|Wrap|Line Numbers
  1. <?
  2. if(!isset($_SESSION)){
  3. session_start();
  4. }
  5.  
  6. if($_SERVER['REQUEST_MEHOD']='post'){
  7. $conn = mysql_connect("myServer","root","");
  8. mysql_select_db("myDB");
  9.  
  10. $result = mysql_query("SELECT * FROM users WHERE user = ".$_POST['username']." AND password = ".$_POST['passwd'],$conn);
  11.  
  12. if(mysql_num_rows($result) == 1){
  13. $_SESSION['user']=$_POST['username'];
  14. $_SESSION['pw']=$_POST['passwd'];
  15.  
  16. header('location: anotherpage.php');
  17. } else {
  18. echo 'failed to login';
  19. }
  20. }
  21.  
  22. mysql_free_result($result);
  23. mysql_close($conn);
  24. ?>
  25.  
  26. <form method='post'>
  27. <input type='text' id='username' name='username' />
  28. <input type='password' id='passwd' name='passwd' />
  29. <input type='submit' />
  30. </form>
on all other pages you use exacly the same code (appart from the form) but instead you use your session variables in your query. also, when you see the SQLquery returns more then one or no row, you use
Expand|Select|Wrap|Line Numbers
  1. header('location: login.php');
in your if structure

what did you do wrong:
your first try came very close. you only forgot that php is executed before page loads so this
Expand|Select|Wrap|Line Numbers
  1. <?php
  2.     if(isset($_POST['ID'])){
  3.         $id = $_POST["ID"];
  4.         $pword = $_POST["pass"];
  5.     }
  6. ?>
needs to be checked in the beginning instead of at the end of your page.

hope that helped

jan
May 19 '09 #7
i did what you said and i have these errors Notice: Undefined index: username in D:\wwwroot\twa09\twa015\assignment2\login1.php on line 18
Notice: Undefined index: passwd in D:\wwwroot\twa09\twa015\assignment2\login1.php on line 18
Warning: mysql_num_rows(): supplied argument is not a valid MySQL result resource in D:\wwwroot\twa09\twa015\assignment2\login1.php on line 20
failed to login
Warning: mysql_free_result(): supplied argument is not a valid MySQL result resource in
Expand|Select|Wrap|Line Numbers
  1. <?xml version="1.0" encoding="UTF-8"?>
  2. <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN"
  3.   "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd"> 
  4.  
  5. <html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en" lang="en">
  6.  
  7. <head>
  8.     <title>login.php</title>
  9.     <?php
  10. if(!isset($_SESSION)){
  11. session_start();
  12. }
  13.  
  14. if($_SERVER['REQUEST_METHOD']='post'){
  15. $conn = mysql_connect("localhost", "TWAassign2", "student");
  16. $db_selected = mysql_select_db('acadcredit', $conn);
  17.  
  18.  
  19. $result = mysql_query("SELECT staffName accessLevel  FROM users WHERE staffID= ".$_POST['username']." AND password = ".$_POST['passwd']);
  20.  
  21. if(mysql_num_rows($result) == 1){
  22. $_SESSION['user']=$_POST['username'];
  23. $_SESSION['pw']=$_POST['passwd'];
  24.  
  25. header('location: selectView.php');
  26. } else {
  27. echo 'failed to login';
  28. }
  29. }
  30.  
  31. mysql_free_result($result);
  32. mysql_close($conn);
  33. ?>
  34. </head>
  35. <body>
  36. <form method='post'>
  37. <p><input type='text' id='username' name='username' /> <br/>
  38. <input type='password' id='passwd' name='passwd' /></p>
  39. <p> <input type='submit' /> </p><input type="reset"  value="Reset" />
  40. </form>
  41. </body>
  42. </html>
  43.  
i need help please
May 19 '09 #8
Ciary
247 Expert 100+
ok my fault i think. try this:
Expand|Select|Wrap|Line Numbers
  1. if($_SERVER['REQUEST_METHOD'] == 'post'){
i forgot to use double 'equals' :S
May 19 '09 #9

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

Similar topics

3
by: koolyio | last post by:
Hey, could you please tell me what is wrong with my login script. I just started learning php. CODE: login.php <? session_start(); header("Cache-Control: private"); ?>
5
by: Simon | last post by:
Hi, I have a Login.php page that logs the user in and out. I has two forms within the page, (depending on what we are trying to do), either one to log in or out. The form calls itself using a...
1
by: Tom Jones | last post by:
Hi, I am using the HttpWebRequest and HttpWebResponse classes to pull information from a web server on the internet. I have an account on one of the webservers that I need to log into...
2
by: Beginner | last post by:
I know this is an old question, but searching all over the internet plus several MS security conferences, still haven't got a straight anwser. Basically, the login.aspx is on one dedicated server...
4
by: nicholas | last post by:
Hi, Got an asp.net application and I use the "forms" authentication mode defined in the web.config file. Everything works fine. But now I would like to add a second, different login page for...
2
by: IdleBrain | last post by:
Hello All: I used a Login control to authenticate a user to login. The problem is that when I login with good username & password, the login view would say that the login was successful. But...
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...
4
tolkienarda
by: tolkienarda | last post by:
Hi all I work for a small webdesign company and we have remote hosting. i built a mysql database with phpmyadmin on the server. i then downloaded and modified a php login page. i am continuing to...
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...
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
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...

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.