Jump to content

learning sessions stuff....confused.


ardyandkari

Recommended Posts

hello,

 

trying to make a login script work...having problems...

 

i basically dont want a whole bunch of pages, just one...(demo.php)

 

i want the page to call itself when the user logs in.

 

i cant get it to do that...probably a screw up of mine.

 

when i fill in the login form, it does nothing...just reloads the login form...

 

code posted below:

<?php 
session_start();
?>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1">
<title>Demonstration</title>
</head>

<body>
<?php include "dbconnect.php"?>
<?PHP 
if ((isset($_POST['user'])) || (isset($_POST['pass']))) {
$user=$_POST['user'];
$pass=$_POST['pass'];

$sql="SELECT * FROM login WHERE username='$user' and password='$pass'";
$result=mysql_query($sql);

// Mysql_num_row is counting table row
$count=mysql_num_rows($result);
// If result matched $myusername and $mypassword, table row must be 1 row

if($count==1){
// Register $myusername, $mypassword and redirect to file "login_success.php"
session_register("$user");
session_register("$pass");

echo "poop is funny";}
else {
echo 
'<table width="300" border="0" align="center" cellpadding="0" cellspacing="1" bgcolor="#CCCCCC">
<tr>
<form name="form1" method="post" action="demo.php">
<td>
<table width="100%" border="0" cellpadding="3" cellspacing="1" bgcolor="#FFFFFF">
<tr>
<td colspan="3"><strong>Member Login </strong></td>
</tr>
<tr>
<td width="78">Username</td>
<td width="6">:</td>
<td width="294"><input name="user" type="text" id="user"></td>
</tr>
<tr>
<td>Password</td>
<td>:</td>
<td><input name="pass" type="text" id="pass"></td>
</tr>
<tr>
<td> </td>
<td> </td>
<td><input type="submit" name="Submit" value="Login"></td>
</tr>
</table>
</td>
</form>
</tr>
</table>';}
?>

</body>
</html>

 

please remember that i am a total noob at php...just trying to learn the basics yet...no training.

 

dont rip me apart......please :'(

Link to comment
Share on other sites

the problem is that you are missing a semi colon from your include statement which would have caused your sql to throw an error, as you did not have a catch error statement you would never know. try this

include "dbconnect.php"; //dont forget the semi colon !
$sql="SELECT * FROM login WHERE username='$user' and password='$pass'";
$result=mysql_query($sql) or die ("Error in query" . mysql_error());// this will throw an error if there is one in the sql

Link to comment
Share on other sites

Couple things I would try

 

<?php 
session_start();
include "dbconnect.php";

if ((isset($_POST['user'])) || (isset($_POST['pass']))) {
$user=$_POST['user'];
$pass=$_POST['pass'];

$sql="SELECT * FROM login WHERE username='$user' and password='$pass'";
$result=mysql_query($sql);

// Mysql_num_row is counting table row
$count=mysql_num_rows($result);
// If result matched $myusername and $mypassword, table row must be 1 row

if($count==1){
// Register $myusername, $mypassword and redirect to file "login_success.php"
$_SESSION['user'] = $user;
$_SESSION['pass'] = $pass;

echo "poop is funny";}
} // close top if
else {
?>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1">
<title>Demonstration</title>
</head>

<body>

<table width="300" border="0" align="center" cellpadding="0" cellspacing="1" bgcolor="#CCCCCC">
<tr>
<form name="form1" method="post" action="demo.php">
<td>
<table width="100%" border="0" cellpadding="3" cellspacing="1" bgcolor="#FFFFFF">
<tr>
<td colspan="3"><strong>Member Login </strong></td>
</tr>
<tr>
<td width="78">Username</td>
<td width="6">:</td>
<td width="294"><input name="user" type="text" id="user"></td>
</tr>
<tr>
<td>Password</td>
<td>:</td>
<td><input name="pass" type="text" id="pass"></td>
</tr>
<tr>
<td> </td>
<td> </td>
<td><input type="submit" name="Submit" value="Login"></td>
</tr>
</table>
</td>
</form>
</tr>
</table>
<?php } ?>

</body>
</html>

Is your poop echo showing up? You had a missing } on your initial If statement. Also if you end up putting in a redirect you will need the code at the top before the html like the code above. Also don't store the password in the session unless it's hashed or encrypted somehow.

Link to comment
Share on other sites

thanks to paul who found my dumb mistake...thanks also to schilly who basically re-wrote the script and made it worked perfectly.

 

i added a little bit to your script though...if you input a username/password that did not exist, it would just load a blank page...

 

i added

if($count!=1) {
echo '<div align="center">INCORRECT USERNAME AND/OR PASSWORD</div>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1">
<title>Demonstration</title>
</head>

<body>

<table width="300" border="0" align="center" cellpadding="0" cellspacing="1" bgcolor="#CCCCCC">
<tr>
<form name="form1" method="post" action="demo.php">
<td>
<table width="100%" border="0" cellpadding="3" cellspacing="1" bgcolor="#FFFFFF">
<tr>
<td colspan="3"><strong>Member Login </strong></td>
</tr>
<tr>
<td width="78">Username</td>
<td width="6">:</td>
<td width="294"><input name="user" type="text" id="user"></td>
</tr>
<tr>
<td>Password</td>
<td>:</td>
<td><input name="pass" type="text" id="pass"></td>
</tr>
<tr>
<td> </td>
<td> </td>
<td><input type="submit" name="Submit" value="Login"></td>
</tr>
</table>
</td>
</form>
</tr>
</table>';}

 

after the first count to make the script kick you back to the main login if it didnt find your username/password.

 

now...how do i go about hashing a password???

 

i will do some reading, but if anyone wants to post an example here that is fine too...thanks so much for your hard work and for helping an idiot do some coding.

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.