Jump to content

Recommended Posts

Hi,

 

I have seem to have already asked this question.  But the script that i used didn't seem to work.

 

This is the script that im using.

 

Login page


<form name="form1" method="post" action="checklogin.php">
<td width="872">
<table width="26%" border="0" cellpadding="3" cellspacing="1" bgcolor="" align="right">
<tr>
<td colspan="3"> </td>
</tr>
<tr>
<td width="58">Username</td>
<td width="3">:</td>
<td width="174"><input name="username" type="text" id="username"></td>
</tr>
<tr>
<td>Password</td>
<td>:</td>
<td><input name="password" type="password" id="password"></td>
</tr>
<tr>
<td> </td>
<td> </td>
<td><input type="submit" name="Submit" value="Login">         <a href="register.html">Register</a></td>
</tr>
</table>
</td>
</form>

 

checklogin page

 


<?php
session_start();
$_SESSION['username'];
?>

<?php

//Database Information

$dbhost = "localhost";
$dbname = "**********";
$dbuser = "**********";
$dbpass = "********";

// Connect to server and select databse.
mysql_connect("$dbhost", "$dbname", "$dbpass")or die("cannot connect");
mysql_select_db("$dbname")or die("cannot select DB");

// username and password sent from form
$myusername=$_POST['username'];
$mypassword=$_POST['password'];

// To protect MySQL injection (more detail about MySQL injection)
$myusername = stripslashes($myusername);
$mypassword = stripslashes($mypassword);
$myusername = mysql_real_escape_string($myusername);
$mypassword = mysql_real_escape_string($mypassword);

$sql="SELECT * FROM members WHERE username='$myusername' and password='$mypassword'";
$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("username");
session_register("password");
echo "login Successful";

}
else {
echo "Wrong Username or Password";
}
?>

 

All of the script that i have done so far works fine.

It is the members only page.

 

Member only page

 


<?php
session_start();
if (isset($_SESSION['username'])) 
  {

  }
else {
echo "SORRY YOU ARE AUTHORIZED TO VEIW THIS SITE!!!"; 
}
?>

 

It comes up with SORRY YOU ARE AUTHORIZED TO VEIW THIS SITE!!!

 

Please will you be able to help me and tell me where i have gone wrong.

 

Thanks for your help,

 

tecmeister

Link to comment
https://forums.phpfreaks.com/topic/107242-solved-members-access-only/
Share on other sites

add the redirect ok also consider md5() for password.....

 

<?php session_start();

$dbhost = "localhost";
$dbname = "**********";
$dbuser = "**********";
$dbpass = "********";

mysql_connect("$dbhost", "$dbname", "$dbpass")or die("cannot connect");
mysql_select_db("$dbname")or die("cannot select DB");

$mypassword = mysql_real_escape_string(stripslashes($_POST['password']));
$myusername = mysql_real_escape_string(stripslashes($_POST['username']));

$sql="SELECT * FROM members WHERE username='$myusername' AND password='$mypassword'";
$result=mysql_query($sql)or die(mysql_error());

if(mysql_num_rows($result)==1){
while($row=mysql_fetch_assoc($result)){

$_SESSION['username']=$row['username'];
$_SESSION['password']=$row['password'];

echo "login Successful";

exit;

}

}else{

echo "Wrong Username or Password";

exit;

}
?>

 

 

 

<?php session_start();

if( (! $_SESSION['username'] )|| (! $_SESSION['password']) ){

echo "SORRY YOU ARE AUTHORIZED TO VEIW THIS SITE!!!"; 

exit;

}else{

echo " user  ".$_SESSION['username']." loged in.... ";

}
?>

that ok that it creates a long set of digits...

 

 

md5 the password before you insert it into the Database... for example...

 

lets say someone enters their password in as "bored"

 

then when you insert it into the db, you actually just insert the md5 of it (23452345234 e.i.)

 

then when they log in, you run the md5 of waht they entered to what is in the database....

Hello,

 

Two Questions:

One - shouldn't the initial warning read 'SORRY YOU ARE NOT AUTHORIZED TO VEIW THIS SITE!!!"

instead of reading [i[sORRY YOU ARE AUTHORIZED TO VEIW THIS SITE!!!"; [/i]

 

TWO: What exactly is 'MD5'? What does it mean? Why would you do this? ('i'm a complete novice so i always have dumb questions.)

 

thanks for any insights!

MD5 is a way of encrypting a string. personally i would use SHA1 hashing because its more secure.

 

in relation to the actual topic:

 

tech, the user does not need to remember the hashed password as long as you hash the password they post from the form.

 

//user registers

so first you would generate a password

encrypt it

store in db

pass gets sent to the user

 

//when a user logs in

post the password to the login script

hash it

retrieve the password from the db

compare the db pass and hashed posted password

set session if they match

 

to enable the user to change their password all you need to do is perform a mysql update query

it is advisable for the change password form to contain three password boxes, current pass, new pass, confirm new pass.

 

//to change a password

user submits the form

hash the old password

check the pass against db pass

check both new passwords match

hash the new password

update the database

 

good luck :)

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.