Connecting Tech Pros Worldwide Help | Site Map

several login attempts

Newbie
 
Join Date: Mar 2008
Posts: 15
#1: Mar 25 '08
hi i'm a newbie in php..
can you help me how to make several login attempt in php?
(if we put wrong password/username,, we will go back to the previous form and we can try 3 more attempts to input the correct password/username before appear an error message)
thanks before..
ronverdonk's Avatar
Moderator
 
Join Date: Jul 2006
Location: The Netherlands
Posts: 4,139
#2: Mar 25 '08

re: several login attempts


Welcome to The Scripts.

We cannot help you if we see no code to help or work on. So, first you'll have to show us the code you have written so far. Then we can have a look at how to limit the number of logins.

And show any code within the appropriate code tags!

Ronald
Newbie
 
Join Date: Mar 2008
Posts: 15
#3: Mar 25 '08

re: several login attempts


sorry I didn't write the code..
here's my code..

this is the form.html
Expand|Select|Wrap|Line Numbers
  1. <html>
  2.     <head>
  3.         <title>form</title>
  4.     </head>
  5.     <body>
  6.         <form action="login.php" method="post">
  7.             <table border=1>
  8.                 <tr>
  9.                     <td colspan=2>
  10.                         Login
  11.                     </td>
  12.                 </tr>
  13.                 <tr>
  14.                     <td>
  15.                         User
  16.                     </td>
  17.                     <td>
  18.                         <input type="text" name="user" value="<user>">
  19.                     </td>
  20.                 </tr>
  21.                 <tr>
  22.                     <td>
  23.                         Password
  24.                     </td>
  25.                     <td>
  26.                         <input type="password" name="pass" value="<pass>">
  27.                     </td>
  28.                 </tr>
  29.                 <tr>
  30.                     <td colspan=2>
  31.                         <input type="hidden" name="ct" value="0">
  32.                         <input type="submit" value="login">
  33.                         &nbsp; &nbsp; &nbsp; &nbsp;
  34.                         <input type="reset" value="cancel">
  35.                     </td>
  36.                 </tr>
  37.             </table>
  38.         </form>
  39.     </body>
  40. </html>
  41.  
this is the login.php
Expand|Select|Wrap|Line Numbers
  1. <html>
  2.     <head>
  3.         <title>login</title>
  4.     </head>
  5.     <body>
  6.         <?
  7.             $nama=$_POST["user"];
  8.             $pswd=$_POST["pass"];
  9.             $count=$_POST["ct"];
  10.             if(($nama=="admin")&&($pswd=="admin")){
  11.                 echo "Selamat datang <b>admin</b>";
  12.             }
  13.             else{
  14.                 if(count<10){
  15.                     $_POST["ct"]++;
  16.                     echo "User atau password yang anda masukkan salah, anda memiliki kesempatan untuk mencoba ";
  17.                     echo 10-$_POST["ct"];
  18.                     echo " kali lagi ";
  19.                 }
  20.                 else{
  21.                     echo "User atau password yang anda masukkan salah, silakan coba lagi atau hubungi customer service";
  22.                 }
  23.             }
  24.         ?>
  25.     </body>
  26. </html>
  27.  
I tried to changed the value of "ct" but it didn't work..
Sorry if my question & my code looks silly, I'm very newb with php..
thanx..
Markus's Avatar
Moderator
 
Join Date: Jun 2007
Location: York, England, with wolves.
Posts: 4,936
#4: Mar 25 '08

re: several login attempts


Hmm.
So you aren't using a database to store log in details, this may make things harder.

What is the purpose of this 'counting of attempts'? Is it so that, after so many tries, the user is blocked from logging in for a certain amount of time?

I mean, you could use sessions, or cookies, but the user could just delete them.. so they're not a great idea. The best bet would be to store the user's IP address in a database, but by the looks of it you havent had a go with MySQL yet.

I recommend you have a look at some php/mysql tutorials - it'll benefit you greatly in the long run.

Regards, markus.
Newbie
 
Join Date: Mar 2008
Posts: 15
#5: Mar 25 '08

re: several login attempts


thanks for you reply markus but I haven't learnt that far..
this is the first day study about php.. and the purpose of this is just for practice..

can we change the value of the input in html using php?
thanks..
Markus's Avatar
Moderator
 
Join Date: Jun 2007
Location: York, England, with wolves.
Posts: 4,936
#6: Mar 25 '08

re: several login attempts


Quote:

Originally Posted by icesh

can we change the value of the input in html using php?
thanks..

Can you explain abit more please?
Newbie
 
Join Date: Mar 2008
Posts: 15
#7: Mar 25 '08

re: several login attempts


<input type="hidden" name="ct" value="0">
^
^
^
can we change the value="0" in form.html form login.php?
another idea that I got is by writing the counter to file.. (but I haven't learnt how to use file in php)
beside that, I haven't got any idea..
thanx
ronverdonk's Avatar
Moderator
 
Join Date: Jul 2006
Location: The Netherlands
Posts: 4,139
#8: Mar 25 '08

re: several login attempts


Since you do not use a database to log login attemps (could be done on IP address), you can store the count in the $_SESSION array and increment it each time the script is re-executed. Like[php]if (isset($_SESSION['count'])) {
$_SESSION['count']++;
if ($_SESSION['count'] > 3) {
// issue message
exit;
}
}
else
$_SESSION['count']=1;
[/php]
Ronald
Newbie
 
Join Date: Mar 2008
Posts: 15
#9: Mar 25 '08

re: several login attempts


thanks ronald..
now I'm reading about $_SESSION from w3school..
I hope it will solve the problem..
I'll give my report if I've try this..
Markus's Avatar
Moderator
 
Join Date: Jun 2007
Location: York, England, with wolves.
Posts: 4,936
#10: Mar 25 '08

re: several login attempts


Quote:

Originally Posted by icesh

thanks ronald..
now I'm reading about $_SESSION from w3school..
I hope it will solve the problem..
I'll give my report if I've try this..

Thing is, as soon as the user closes the browser, the sessions are lost.
Defeating the error.
Newbie
 
Join Date: Mar 2008
Posts: 15
#11: Mar 25 '08

re: several login attempts


thanks to all of you..
the $_SESSION work very well, here's my code..
Expand|Select|Wrap|Line Numbers
  1. <html>
  2.     <head>
  3.         <title>form</title>
  4.     </head>
  5.     <body>
  6.         <form action="login.php" method="post">
  7.             <table border=1>
  8.                 <tr>
  9.                     <td colspan=2>
  10.                         Login
  11.                     </td>
  12.                 </tr>
  13.                 <tr>
  14.                     <td>
  15.                         User
  16.                     </td>
  17.                     <td>
  18.                         <input type="text" name="user" value="<user>">
  19.                     </td>
  20.                 </tr>
  21.                 <tr>
  22.                     <td>
  23.                         Password
  24.                     </td>
  25.                     <td>
  26.                         <input type="password" name="pass" value="<pass>">
  27.                     </td>
  28.                 </tr>
  29.                 <tr>
  30.                     <td colspan=2>
  31.                         <input type="submit" value="login">
  32.                         &nbsp; &nbsp; &nbsp; &nbsp;
  33.                         <input type="reset" value="cancel">
  34.                     </td>
  35.                 </tr>
  36.             </table>
  37.         </form>
  38.     </body>
  39. </html>
  40.  
Expand|Select|Wrap|Line Numbers
  1. <?
  2.     session_start();
  3. ?>
  4.  
  5. <html>
  6.     <head>
  7.         <title>login</title>
  8.     </head>
  9.     <body>
  10.         <?
  11.             $nama=$_POST["user"];
  12.             $pswd=$_POST["pass"];
  13.             if(($nama=="admin")&&($pswd=="admin")){
  14.                 echo "Selamat datang <b>admin</b>";
  15.                 unset($_SESSION['ct']);
  16.             }
  17.             else{
  18.                 if(isset($_SESSION['ct'])){
  19.                     if($_SESSION['ct']>5){
  20.                         echo "Anda salah memasukkan user atau password sebanyak 5 kali, ip anda diblok selama 15 menit";
  21.                         unset($_SESSION['ct']);
  22.                     }
  23.                     else{
  24.                         echo "User atau password yang anda masukkan salah, anda memiliki kesempatan untuk mencoba ";
  25.                         echo 6-$_SESSION["ct"];
  26.                         echo " kali lagi";
  27.                         echo "<br><a href='form.html'>kembali ke form</a>";
  28.                     }
  29.                     $_SESSION['ct']++;
  30.                 }
  31.                 else{
  32.                     $_SESSION['ct']=1;
  33.                 }
  34.             }
  35.         ?>
  36.     </body>
  37. </html>
  38.  
I've another question,, can we destroy SESSION when we close the tab?
ronverdonk's Avatar
Moderator
 
Join Date: Jul 2006
Location: The Netherlands
Posts: 4,139
#12: Mar 25 '08

re: several login attempts


[php]unset['$_SESSION'];
$_SESSION = array();[/php]See you next time.
Ronald
Newbie
 
Join Date: Mar 2008
Posts: 15
#13: Mar 25 '08

re: several login attempts


I'm sorry Ronald, maybe my question is not clear enough..
when we open a browser,, we can open a lot of tabs..
my question is,, is there any code so that, when we close the tab (login.php), the SESSION will be destroy.. (without close the browser)
thanx..
ronverdonk's Avatar
Moderator
 
Join Date: Jul 2006
Location: The Netherlands
Posts: 4,139
#14: Mar 25 '08

re: several login attempts


You can either close the entire $_SESSION array, I showed that. You can also unset any individual entries that you have set e.g.[php]unset[$_SESSION['count']);[/php]or do I still not understand it?

Ronald
Newbie
 
Join Date: Mar 2008
Posts: 15
#15: Mar 25 '08

re: several login attempts


I use the unset($_SESSION['ct']);
when the attempt is more than 10..
the problem is,, when I close the tab (login.php) and open it again,, the last $_SESSION['ct'] still remain..
where should I add unset($_SESSION['ct']); so that, the $_SESSION['ct'] will be cleared when I close the tab..
I've tried what markus said (when we close the browser, the $_SESSION['ct']) will be automatically cleared.., but when I close the tab without close the browser, the last $_SESSION['ct'] still remain..
I'm sorry to to make such trouble..
TheServant's Avatar
Expert
 
Join Date: Feb 2008
Location: Australia
Posts: 913
#16: Mar 25 '08

re: several login attempts


No trouble. I don't think it's possible to do what you want. Basically with Ronalds way ( or mine - I prefer session_destroy() ) the user would have to load the "session destroying page" to end the session.

I didn't think about closing tabs ending sessions, but if you say they don't then that means that if the user closes the tab he will not have run your script and will continue to be logged in or what ever.

Why do you want to log them out when they close the tab? I don't think it's possible with sessions, and I can't think of another way to do it.
Newbie
 
Join Date: Mar 2008
Posts: 15
#17: Mar 26 '08

re: several login attempts


I just wondering if we can do that, but you're right,, we don't need to start a new session when we close a tab.., I'm sorry..
thank you guys..
this thread can be closed, my question has been answered..
special thanks for Ronald,Markus & "TheServant"
TheServant's Avatar
Expert
 
Join Date: Feb 2008
Location: Australia
Posts: 913
#18: Mar 26 '08

re: several login attempts


Glad I could help. Hope to see you back here.
ronverdonk's Avatar
Moderator
 
Join Date: Jul 2006
Location: The Netherlands
Posts: 4,139
#19: Mar 27 '08

re: several login attempts


The trio is always available here to help!

Ronald
Reply