Page 1 of 1

PHP login/sign up code!

Posted: Mon May 04, 2009 10:20 am
by snarkiest
Hi,
I have went trough a nightmare to get this code because I am really pure on PHP and now I have a small question.

Code: Select all

<form name="loginf" action="../test/loginck.php" method="post">
                <tr>
                  <td height="" style="padding-top:5px;padding-bottom:5px;"><table width="151" border="0" cellspacing="0" cellpadding="0">
                      <tr>
                        <td align="center"><span id="login"><span>Login</span></span></td>
                      </tr>
                      <tr>
                        <td align="center" class="lrbg" width="151"><table width="141" border="0" cellspacing="0" cellpadding="0">
                            <tr>
                              <td><div class="text">Username:</div>
                                <input name="userid" type="text" class="text" size="22" ></td>
                            </tr>
                            <tr>
                              <td><div class="text">Password:</div>
                                <input name="password" type="password" class="text" size="22" ></td>
                            </tr>
                            <tr>
                              <td align="center" style="padding-top:3px;padding-bottom:3px;"><table width="0" border="0" cellspacing="0" cellpadding="0">
                                  <tr>
                                    <td><span id="login_form_tp"></span></td>
                                  </tr>
                                  <tr>
                                    <td class="login_form_bg" align="center"><a href="javascript&#058;document.forms['loginf'].submit();" class="link">Log in</a></td>
                                  </tr>
                                  <tr>
                                    <td><span id="login_form_bt"></span></td>
                                  </tr>
                                </table></td>
                            </tr>
                            <tr>
                              <td align="center" style="padding-top:3px;padding-bottom:3px;"><table width="0" border="0" cellspacing="0" cellpadding="0">
                                  <tr>
                                    <td><span id="login_form_tp"></span></td>
                                  </tr>
                                  <tr>
                                    <td class="login_form_bg" align="center"><a href="javascript&#058;alert('Registration is not allowed yet!')" class="link">Register</a></td>
                                  </tr>
                                  <tr>
                                    <td><span id="login_form_bt"></span></td>
                                  </tr>
                                </table></td>
                            </tr>
                          </table></td>
                      </tr>
                      <tr>
                        <td align="center"><span id="lr_bt"></span></td>
                      </tr>
                    </table></td>
                </tr>
              </form>
So this is the log in form and it on HTML file.

Code: Select all

<?
include "include/session.php";
 
$dbservertype='mysql';
$servername='localhost';
// 
$dbusername='';
$dbpassword='';
// 
$dbname='fans_db';
 
//
connecttodb($servername,$dbname,$dbusername,$dbpassword);
function connecttodb($servername,$dbname,$dbuser,$dbpassword)
{
global $link;
$link=mysql_connect ("$servername","$dbuser","$dbpassword");
if(!$link){die("Couldn't connect to MySQL");}
mysql_select_db("$dbname",$link) or die ("Couldn't find database.".mysql_error());
}
//
 
?>
<!doctype html public "-//w3c//dtd html 3.2//en">
 
<html>
 
<head>
<title>Test log in</title>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<meta name="GENERATOR" content="Arachnophilia 4.0">
<meta name="FORMATTER" content="Arachnophilia 4.0">
</head>
 
<body bgcolor="#ffffff" text="#000000" link="#0000ff" vlink="#800080" alink="#ff0000">
<?
$userid=mysql_real_escape_string($userid);
$password=mysql_real_escape_string($password);
 
if($rec=mysql_fetch_array(mysql_query("SELECT * FROM plus_signup WHERE userid='$userid' AND password = '$password'"))){
    if(($rec['userid']==$userid)&&($rec['password']==$password)){
     include "include/newsession.php";
            echo "<p class=data> <center>Successfully,Logged in<br><br><a href='logout.php'> Log OUT </a><br><br><a href=welcome.php>Click here if your browser is not redirecting automatically or you don't want to wait.</a><br></center>";
     print "<script>";
       print " self.location='welcome.php';";
          print "</script>";
 
                } 
        }   
    else {
 
        session_unset();
echo "<font face='Verdana' size='2' color=red>Wrong Login. Use your correct  Userid and Password and Try <br><center><input type='button' value='Retry' onClick='history.go(-1)'></center>";
        
    }
?>
</body>
 
</html>
 
And this is the .php file which is used for log in.
I haven't created the database yet, but the question is not about that.

I want to show these messages (Wrong Login... Successfully,Logged...) on .html file, but I think they won't show there because there is no return script or something.

What should I do to create clear log in page where on log in page under the log in form would show those messages (wrong login or password) and after successful login the log in window form don't show? I want it on html file but I can move to .php too.

Instruct me please what and how to do? :roll:

Re: PHP login/sign up code!

Posted: Mon May 04, 2009 3:24 pm
by infolock
The best solution is to start small..

Set your current code aside and create 4 new files: login.php, authenticate.php, success.php, failed.php

login.php should just be a regular form for entering the username/password

Set action for authenticate.php

Check the credentials in authenticate.php. On success, redirect them with like the header command, to the success.php file. If they don't authenticate, then redirect them to failed.php page

Once you get that logic down pat, go back to those other files you set aside and see if it makes a little more sense.