Already you know about validate login form with remember me and signout concepts using php. You want to know Login with remember me and signout using php and mysql.
Now see about how to send forgotten password to mail using php?. First your table contains users mail id. Just imagine this is your table:
Consider the following example:
<html>
While you run the above file, you'll get output like this:
<html>
<head>
<style type="text/css">
input{
border:1px solid olive;
border-radius:5px;
}
h1{
color:darkgreen;
font-size:22px;
text-align:center;
}
</style>
</head>
<body>
<h1>Forgot Password<h1>
<form action='#' method='post'>
<table cellspacing='5' align='center'>
<tr><td>Email id:</td><td><input type='text' name='mail'/></td></tr>
<tr><td></td><td><input type='submit' name='submit' value='Submit'/></td></tr>
</table>
</form>
<?php
if(isset($_POST['submit']))
{
mysql_connect('localhost','root','') or die(mysql_error());
mysql_select_db('new') or die(mysql_error());
$mail=$_POST['mail'];
$q=mysql_query("select * from login where mail='".$mail."' ") or die(mysql_error());
$p=mysql_affected_rows();
if($p!=0)
{
$res=mysql_fetch_array($q);
$to=$res['mail'];
$subject='Remind password';
$message='Your password : '.$res['password'];
$headers='From:guruparthiban19@gmail.com';
$m=mail($to,$subject,$message,$headers);
if($m)
{
echo'Check your inbox in mail';
}
else
{
echo'mail is not send';
}
}
else
{
echo'You entered mail id is not present';
}
}
?>
</body>
</html>
Where,
mysql_query("select * from login where mail='".$mail."' "); is select the values from mysql table.
mysql_affected_rows() return the number of rows selected. If it returns 0, then there is no values is present in mysql table.
mysql_fetch_array() return the results an associated array. You want to know mysql_fetch_array().
Mail() is used to send password to mail. Yow want to know mail() in php.
When you click the 'forgot password' in login page, you'll get output like this:
You entered your email id in text box and click submit button. Now you can get message 'Check your inbox in mail' if the mail is send. Otherwise, you'll get 'Mail is not send' message. If you enetered mail id which is not present is mysql table, then you will get 'You entered mail is is not present' message.
Now see about how to send forgotten password to mail using php?. First your table contains users mail id. Just imagine this is your table:
Login form using php and mysql:
Consider the following example:
login.php:
<html>
<head>
<style type="text/css">
input{
border:1px solid olive;
border-radius:5px;
}
h1{
color:darkgreen;
font-size:22px;
text-align:center;
}
span{
color:lightgreen;
}
</style>
</head>
<body>
<h1>Login<h1>
<form action='#' method='post'>
<table cellspacing='5' align='center'>
<tr><td>User name:</td><td><input type='text' name='name'/></td></tr>
<tr><td>Password:</td><td><input type='password' name='pwd'/></td></tr>
<tr><td></td><td><input type='checkbox' name='remember' /> <span>Remember me</span></td></tr>
<tr><td></td><td> <span><a href="forgot.php">Forgot password</a></span></td></tr>
<tr><td></td><td><input type='submit' name='submit' value='Submit'/></td></tr>
</table>
</form>
<?php
session_start();
//your values are stored in cookies, then you can login without validate
if(isset($_COOKIE['name']) && isset($_COOKIE['pwd']))
{
header('location:welcome.php');
}
// login validation in php
if(isset($_POST['submit']))
{
mysql_connect('localhost','root','') or die(mysql_error());
mysql_select_db('new') or die(mysql_error());
$name=$_POST['name'];
$pwd=$_POST['pwd'];
if($name!=''&&$pwd!='')
{
$query=mysql_query("select * from login where name='".$name."' and password='".$pwd."' ") or die(mysql_error());
$res=mysql_fetch_row($query);
if($res)
{
if(isset($_POST['remember']))
{
setcookie('name',$name, time() + (60*60*24*1));
setcookie('pwd',$pwd, time() + (60*60*24*1));
}
$_SESSION['name']=$name;
header('location:welcome.php');
}
else
{
echo'You entered username or password is incorrect';
}
}
else
{
echo'Enter both username and password';
}
}
?>
</body>
</html>
<style type="text/css">
input{
border:1px solid olive;
border-radius:5px;
}
h1{
color:darkgreen;
font-size:22px;
text-align:center;
}
span{
color:lightgreen;
}
</style>
</head>
<body>
<h1>Login<h1>
<form action='#' method='post'>
<table cellspacing='5' align='center'>
<tr><td>User name:</td><td><input type='text' name='name'/></td></tr>
<tr><td>Password:</td><td><input type='password' name='pwd'/></td></tr>
<tr><td></td><td><input type='checkbox' name='remember' /> <span>Remember me</span></td></tr>
<tr><td></td><td> <span><a href="forgot.php">Forgot password</a></span></td></tr>
<tr><td></td><td><input type='submit' name='submit' value='Submit'/></td></tr>
</table>
</form>
<?php
session_start();
//your values are stored in cookies, then you can login without validate
if(isset($_COOKIE['name']) && isset($_COOKIE['pwd']))
{
header('location:welcome.php');
}
// login validation in php
if(isset($_POST['submit']))
{
mysql_connect('localhost','root','') or die(mysql_error());
mysql_select_db('new') or die(mysql_error());
$name=$_POST['name'];
$pwd=$_POST['pwd'];
if($name!=''&&$pwd!='')
{
$query=mysql_query("select * from login where name='".$name."' and password='".$pwd."' ") or die(mysql_error());
$res=mysql_fetch_row($query);
if($res)
{
if(isset($_POST['remember']))
{
setcookie('name',$name, time() + (60*60*24*1));
setcookie('pwd',$pwd, time() + (60*60*24*1));
}
$_SESSION['name']=$name;
header('location:welcome.php');
}
else
{
echo'You entered username or password is incorrect';
}
}
else
{
echo'Enter both username and password';
}
}
?>
</body>
</html>
While you run the above file, you'll get output like this:
Fogot password in php:
The php script for send forgotten password to mail is:
forgot.php:
<html>
<head>
<style type="text/css">
input{
border:1px solid olive;
border-radius:5px;
}
h1{
color:darkgreen;
font-size:22px;
text-align:center;
}
</style>
</head>
<body>
<h1>Forgot Password<h1>
<form action='#' method='post'>
<table cellspacing='5' align='center'>
<tr><td>Email id:</td><td><input type='text' name='mail'/></td></tr>
<tr><td></td><td><input type='submit' name='submit' value='Submit'/></td></tr>
</table>
</form>
<?php
if(isset($_POST['submit']))
{
mysql_connect('localhost','root','') or die(mysql_error());
mysql_select_db('new') or die(mysql_error());
$mail=$_POST['mail'];
$q=mysql_query("select * from login where mail='".$mail."' ") or die(mysql_error());
$p=mysql_affected_rows();
if($p!=0)
{
$res=mysql_fetch_array($q);
$to=$res['mail'];
$subject='Remind password';
$message='Your password : '.$res['password'];
$headers='From:guruparthiban19@gmail.com';
$m=mail($to,$subject,$message,$headers);
if($m)
{
echo'Check your inbox in mail';
}
else
{
echo'mail is not send';
}
}
else
{
echo'You entered mail id is not present';
}
}
?>
</body>
</html>
Where,
mysql_query("select * from login where mail='".$mail."' "); is select the values from mysql table.
mysql_affected_rows() return the number of rows selected. If it returns 0, then there is no values is present in mysql table.
mysql_fetch_array() return the results an associated array. You want to know mysql_fetch_array().
Mail() is used to send password to mail. Yow want to know mail() in php.
You entered your email id in text box and click submit button. Now you can get message 'Check your inbox in mail' if the mail is send. Otherwise, you'll get 'Mail is not send' message. If you enetered mail id which is not present is mysql table, then you will get 'You entered mail is is not present' message.
nice post...
ReplyDeletethanx for sharing..
Yes it's a good script
Deletethis type in programming resource(4) of type (mysql link) you entered username and password is incorrect
ReplyDeletethanks boss reduce my work this code
ReplyDeleteCode is very nice but it's not showing in my mail
ReplyDeleteJust you run this code in server, you can receive mail.
Deleteyou have to configure smtp
Deletesir i have sent the problem and uptil now i did not recieve the reply... zakirkhan90089
DeleteThanks it's work! appreciate it so much!
ReplyDeleteThanks very much this code just save me
ReplyDeleteWerey
DeleteI tried this code but i received this error..Warning: mail() [function.mail]: Failed to connect to mailserver at "server.com" port 25, verify your "SMTP" and "smtp_port" setting in php.ini or use ini_set(). How can i fix this?
ReplyDeleteYou can send mail only from server.
Deletesir i have paste the same code in my project but cause some error
Deletesir the message is shown on page but email is not sent. i m confused in code like $from:"emailaddress of whom";
Delete$to =" to whom";
hi bro code is working fine but when i click submit button the mail is not sent to mail address . i m using xammp server i run this code on localhost. but mail is sent . how can i get the mail from that code contaning user password
ReplyDeleteYou can't send mail from local. So you can test this feature at server.
Deletecan you sent me email on my email
Deletezakirkhan90089@gmail.com
Thank you so much for this my friend, just remember guys, your site must be online and hosted for the mail function to work.
ReplyDeleteWorked like a charm for me. :)
When I try this (it's hosted on my webserver) I do not get an email. The funny thing is if I set the "to" field to an email address with the same domain as the site it works, like for example if I try sending to someone@mydomain.com or someotheraddress@mydomain.com (and the "from" is coming from mydomain) it will go through. When I try sending to someone@gmail.com or someone@somewhereelse.com it does not go through.
ReplyDeleteI get the following error: Warning: mail(): SMTP server response: 530 SMTP authentication is required. in E:\HostingSpaces\graffix\myserverpath
It seems like it will only send emails to my domain from someone (my contact form works fine, the user is the "from" address and my email is the "to") but not when I'm trying to send someone else an email from my domain.
Any ideas?
Fatal error: Call to undefined function email() in /home/sos/public_html/smtp/forgot.php on line 40
ReplyDelete??
You can send mail only from your server not from local
Deletegud work...................
ReplyDeletewhat do u mean by server????
ReplyDeleteThis comment has been removed by the author.
ReplyDeleteCheck your inbox in mail, is on browser but no mail recieved
ReplyDeleteYou can send mail only from your server. Not from local system. If u want to send mail from your local system, then you need to setup SMTP port php.ini file and also you need a net connections.
DeleteThis comment has been removed by the author.
DeleteHow i set up my smtp port in my php.ini file can you tell me.
DeleteThanks Bro
ReplyDeletehello sir this worked for me thankyou verymuch sir ji I am beginner here.. sir i want your little help... i want to send multiple checkbox value in mail but i am getting an ERROR:- "Warning: trim() expects parameter 1 to be string, array given" PLEASE HELP me sir.. thank you
ReplyDeleteWe can use trim() only for trim space or any values in a string. Please check your array whether it have values or not? Then pass array values into trim().
Deletesir is it working on localhost or not...i m getting aal the specified messeges but not getting password in my mail...
ReplyDeleteplz help me sir...
thanks in advance
Did you confirm you received mail from your localhost? If yes, then please check your MySQL part to get password.
Deletehow to session destory?
ReplyDeletePlease refer this http://www.phponwebsites.com/2014/05/php-session.html
DeleteI have everything setup including smtp configuration but still when I submit I get "mail not send". I submit the correct email address. I am using wampserver and I have put it Online. What could be the problem? please help. Otherwise the codes are good, thank you
ReplyDeletecool its working successfully . ty
ReplyDeleteplz tell how to work this code.mail is display in mail server.
Deletehow can we reset password for above code??
ReplyDeletecan you share the link or code?
In this tutorial, the above code snippet only send password to corresponding mail address. So the user can see his password in his inbox.
DeleteIf you want to reset password, first get password from user and then update that password to login table. The MySQL Query is:
"update login set password='PASSWORD' where email='MAIL' and name='NAME';"
hi i get this error on server..
ReplyDeleteWarning: mail() has been disabled for security reasons in D:\Inetpub\vhosts\anandindustries1985.com\d.digitalvichar.com\forget_pwd.php on line 39
mail is not send
Hi sir The code was successfully work i use it in my server but when i got the check in your mail box there was no an Message. Help
ReplyDelete( ! ) Warning: mail(): Failed to connect to mailserver at "localhost" port 25, verify your "SMTP" and "smtp_port" setting in php.ini or use ini_set() in C:\wamp\www\blood\resetpwd.php ............show this eror plz solve it
ReplyDeletehi,everyone
ReplyDeletei am using this code and work successfully but i got warning():530 authentication is required..
please any help me..?
nice code ,this code very usedfull for me..
ReplyDeleteThis comment has been removed by the author.
DeleteSir i have uploaded my page on server ..
ReplyDeleteit is successfully running the code but when i check mail in mail server (gmail.com) , it does not display any mail. help me sir
Hi All,
ReplyDeleteI've the following error messages:
Notice: Undefined index: email in ..\forgot.php on line 29
Fatal error: Call to undefined function email() in ..\forgot.php on line 39
HI Boss, this code has been helpful. However, it worked for a few times then it went off. Never send email, never display any error, just blank white page that appears and thats it. Any help will be very much appreciated.
ReplyDeleteThanks.
Zollyzo
hii...nice code working properly...
ReplyDeleteThank you bro.....
it's just printing email not sent y s it so?please helpme
ReplyDeletesir apka code web server run nhi kr rha hai submit pr page blank ho jaah rha hai
ReplyDeleteThis comment has been removed by the author.
ReplyDeleteBefore selecting a mailing house make sure to do some serious research: The relationship a direct mailer develops with their direct news house is crucial to the success of the company's direct mail shots so proceed with care. Here are some of the things which will help you when looking for the right mailing services company.
ReplyDeleteKeep in touch whilst functioning from your own home office with out all of the hassle of purchasing or procurment costly office equipment. Debtors are allowed to apply with their a bad credit score background whenever. Autonomous
ReplyDeleteWarning: mail(): Failed to connect to mailserver at "localhost" port 25, verify your "SMTP" and "smtp_port" setting in php.ini or use ini_set() in C:\wamp\www\onlineblood\ForgotPassword.php on line 75
ReplyDelete