login php | Member | | Join Date: Apr 2009
Posts: 63
| |
i would like to create php login form but the problem is the form doesnt do anything -
<?php require_once("nocache.php"); ?>
-
-
<?xml version="1.0" encoding="UTF-8"?>
-
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN"
-
"http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
-
<html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en" lang="en">
-
<head>
-
<title>login.php</title>
-
<?php
-
if(isset($_POST['Submit']))
-
if (empty($id) || empty($pword)){
-
header("location: login.php");
-
}else{
-
$conn = mysql_connect("localhost", "TWAassign2", "student");
-
if(!$conn){
-
die('Conection could not be opened: ' . mysql_error());
-
}
-
$db_selected = mysql_select_db('acadcredit', $conn);
-
$sql = "SELECT staffID, accessLevel FROM staff WHERE id = '$id' AND password = '$pword'";
-
$rs = mysql_query($sql, $conn);
-
if (mysql_num_rows($rs)> 0 ) {
-
session_start();
-
$_SESSION["who"] = $id;
-
$_SESSION["level"] = mysql_result($rs,0,"level");
-
header("location: selectView.php");
-
}else{
-
header("location: login.php");
-
}
-
}
-
?>
-
-
</head>
-
<body>
-
<form method="post" action="login.php">
-
<h1> Welcome to Login Page </h1>
-
<h2> Please enter Your Staff ID and password- Thanks</h2>
-
<p>Staff ID <input type="text" name="ID" /><br/>
-
Password <input type="password" name="pass" /></p>
-
<p>
-
<?php
-
if(isset($_POST['ID'])){
-
$id = $_POST["ID"];
-
$pword = $_POST["pass"];
-
}
-
?>
-
-
<input type="submit" value="login" />
-
<input type="reset" value="Reset" /></p>
-
</form>
-
</body>
-
</html>
-
why isnt working
thanks
|  | Familiar Sight | | Join Date: May 2009 Location: Wellington, New Zealand
Posts: 152
| | | re: login php -
-
<?php session_start();
-
// have to initialised session_start(); top of the page
-
?>
-
<?xml version="1.0" encoding="UTF-8"?>
-
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN"
-
"http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
-
<html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en" lang="en">
-
<head>
-
<title>login.php</title>
-
<?php
-
if(isset($_POST['Submit'])){
-
-
$id= $_POST['ID'];
-
$pword= $_POST['pass'];
-
-
}
-
if (empty($id) || empty($pword)){
-
header("location: login.php");
-
}else{
-
$conn = mysql_connect("localhost", "TWAassign2", "student");
-
if(!$conn){
-
die('Conection could not be opened: ' . mysql_error());
-
}
-
$db_selected = mysql_select_db('acadcredit', $conn);
-
$sql = "SELECT staffID, accessLevel FROM staff WHERE id = '$id' AND password = '$pword'";
-
$rs = mysql_query($sql, $conn);
-
if (mysql_num_rows($rs)> 0 ) {
-
session_start();
-
$_SESSION["who"] = $id;
-
$_SESSION["level"] = mysql_result($rs,0,"level");
-
header("location: selectView.php");
-
}else{
-
header("location: login.php");
-
}
-
}
-
?>
-
-
</head>
-
<body>
-
<form method="post" action="login.php">
-
<h1> Welcome to Login Page </h1>
-
<h2> Please enter Your Staff ID and password- Thanks</h2>
-
<p>Staff ID <input type="text" name="ID" /><br/>
-
Password <input type="password" name="pass" /></p>
-
<p>
-
<!-- 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
-
initialised,
-
-
-->
-
<?php
-
/*
-
if(isset($_POST['ID'])){
-
$id = $_POST["ID"];
-
$pword = $_POST["pass"];
-
}*/
-
?>
-
-
<input type="submit" value="login" />
-
<input type="reset" value="Reset" /></p>
-
</form>
-
</body>
-
</html>
-
-
-
| | Member | | Join Date: Apr 2009
Posts: 63
| | | re: login php
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
|  | Familiar Sight | | Join Date: May 2009 Location: Wellington, New Zealand
Posts: 152
| | | re: login php
try this and if any error come mention that -
<?php session_start();
-
// have to initialised session_start(); top of the page
-
?>
-
-
<html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en" lang="en">
-
<head>
-
<title>login.php</title>
-
</head>
-
<body >
-
<br><br>
-
-
-
<?
-
-
if(isset($_POST['submit'])){
-
-
$id= $_POST['ID'];
-
$pword= $_POST['pass'];
-
/echo "password".$pword;
-
//echo "id".$id;
-
}
-
if (empty($id) || empty($pword)){
-
header("location: login.php");
-
}else{
-
$conn = mysql_connect("localhost", "TWAassign2", "student");
-
if(!$conn){
-
die('Conection could not be opened: ' . mysql_error());
-
}
-
$db_selected = mysql_select_db('acadcredit', $conn);
-
$sql = "SELECT staffID, accessLevel FROM staff WHERE id = '$id' AND password = '$pword'";
-
$rs = mysql_query($sql, $conn);
-
if (mysql_num_rows($rs)> 0 ) {
-
-
$_SESSION["who"] = $id;
-
$_SESSION["level"] = mysql_result($rs,0,"level");
-
header("location: selectView.php");
-
}else{
-
header("location: login.php");
-
}
-
}
-
?>
-
<!--=============================================================================-->
-
<form method="post" action="<?php echo $_SERVER['PHP_SELF']; ?>">
-
<h1> Welcome to Login Page </h1>
-
<h2> Please enter Your Staff ID and password- Thanks</h2>
-
<p>Staff ID <input type="text" name="ID" /><br/>
-
Password <input type="password" name="pass" /></p>
-
-
<input type="submit" value="login" />
-
<input type="reset" value="Reset" /></p>
-
</form>
-
-
-
-
</body>
-
</html>
-
-
-
-
| | Member | | Join Date: Apr 2009
Posts: 63
| | | re: login php
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
| | Member | | Join Date: Apr 2009
Posts: 63
| | | re: login php
i realy need help to create the login page as soon as possible please help
thanks
|  | Expert | | Join Date: Apr 2009 Location: The outer ring of hell
Posts: 238
| | | re: login php
creating a login is really simple: - <?
-
if(!isset($_SESSION)){
-
session_start();
-
}
-
-
if($_SERVER['REQUEST_MEHOD']='post'){
-
$conn = mysql_connect("myServer","root","");
-
mysql_select_db("myDB");
-
-
$result = mysql_query("SELECT * FROM users WHERE user = ".$_POST['username']." AND password = ".$_POST['passwd'],$conn);
-
-
if(mysql_num_rows($result) == 1){
-
$_SESSION['user']=$_POST['username'];
-
$_SESSION['pw']=$_POST['passwd'];
-
-
header('location: anotherpage.php');
-
} else {
-
echo 'failed to login';
-
}
-
}
-
-
mysql_free_result($result);
-
mysql_close($conn);
-
?>
-
-
<form method='post'>
-
<input type='text' id='username' name='username' />
-
<input type='password' id='passwd' name='passwd' />
-
<input type='submit' />
-
</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 - 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 - <?php
-
if(isset($_POST['ID'])){
-
$id = $_POST["ID"];
-
$pword = $_POST["pass"];
-
}
-
?>
needs to be checked in the beginning instead of at the end of your page.
hope that helped
jan
| | Member | | Join Date: Apr 2009
Posts: 63
| | | re: login php
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 -
<?xml version="1.0" encoding="UTF-8"?>
-
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN"
-
"http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
-
-
<html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en" lang="en">
-
-
<head>
-
<title>login.php</title>
-
<?php
-
if(!isset($_SESSION)){
-
session_start();
-
}
-
-
if($_SERVER['REQUEST_METHOD']='post'){
-
$conn = mysql_connect("localhost", "TWAassign2", "student");
-
$db_selected = mysql_select_db('acadcredit', $conn);
-
-
-
$result = mysql_query("SELECT staffName accessLevel FROM users WHERE staffID= ".$_POST['username']." AND password = ".$_POST['passwd']);
-
-
if(mysql_num_rows($result) == 1){
-
$_SESSION['user']=$_POST['username'];
-
$_SESSION['pw']=$_POST['passwd'];
-
-
header('location: selectView.php');
-
} else {
-
echo 'failed to login';
-
}
-
}
-
-
mysql_free_result($result);
-
mysql_close($conn);
-
?>
-
</head>
-
<body>
-
<form method='post'>
-
<p><input type='text' id='username' name='username' /> <br/>
-
<input type='password' id='passwd' name='passwd' /></p>
-
<p> <input type='submit' /> </p><input type="reset" value="Reset" />
-
</form>
-
</body>
-
</html>
-
i need help please
|  | Expert | | Join Date: Apr 2009 Location: The outer ring of hell
Posts: 238
| | | re: login php
ok my fault i think. try this: - if($_SERVER['REQUEST_METHOD'] == 'post'){
i forgot to use double 'equals' :S
|  | | | | /bytes/about
We are a network of experts and professionals in IT and software development that help one another with answers to tough questions and share insights.
Get the best answers to your questions from over 226,510 network members.
|