0% found this document useful (0 votes)
103 views

Include Include

The document describes a PHP login system that allows users to be added to a database. The system includes: 1) An "add a user" page that collects a username and password and inserts it into the database table. 2) PHP files that connect to the database, retrieve the existing user, and insert or update the user details. 3) A login page that authenticates users by matching the username and password to the database. 4) Additional files that start sessions for authenticated users and verify sessions on protected pages.

Uploaded by

Kristine Ferry
Copyright
© Attribution Non-Commercial (BY-NC)
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
103 views

Include Include

The document describes a PHP login system that allows users to be added to a database. The system includes: 1) An "add a user" page that collects a username and password and inserts it into the database table. 2) PHP files that connect to the database, retrieve the existing user, and insert or update the user details. 3) A login page that authenticates users by matching the username and password to the database. 4) Additional files that start sessions for authenticated users and verify sessions on protected pages.

Uploaded by

Kristine Ferry
Copyright
© Attribution Non-Commercial (BY-NC)
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 6

addauser.php <?

php $page_title="Add a user - FREE PHP code and SQL"; $page_keywords="free php, sql insert record"; $page_desc="Add a username and password to test free online login PHP MySQL example system"; include("header.inc"); ?> <?php include("validateform.inc"); ?> <p align="center"><big><strong>3. Add a User</strong></p></big> <blockquote> <blockquote> <p>Below is the current single record within the database: </p> </blockquote> <blockquote> <blockquote> <?php include("config.php"); ?> <?php include("getrecord.inc"); ?> </blockquote> </blockquote> <blockquote> <p>Add your own username and password, enter your details below and click save.&nbsp; <strong>DO NOT</strong> use valid details as the information you enter will be displ ayed above and the next visitor will be able to view them as you are now.</p> </blockquote> </blockquote> <form ACTION="savedata.php" name="saveform" METHOD="POST" align="center"> <div align="center"><center><table border="0" width="93%" cellspacing="0" c ellpadding="0"> <tr> <td width="52%"><div align="center"><center><table border="0" height="5 9" width="310" bgcolor="#808080" cellspacing="1" cellpadding="0"> <tr> <td width="248" height="19" bgcolor="#C0C0C0" align="right"><p><fo nt color="#000000"><small>Add Your test username:</small></font></td> <td width="123" height="19" bgcolor="#C0C0C0"><p><input NAME="usern ame" VALUE SIZE="8" MAXLENGTH="16" tabindex="1"></td> <td width="47" height="19" align="center" bgcolor="#C0C0C0"><div al ign="center"><center><p><a href="javascript:alert('The username must be between 4 and 16 chara cters long.')"><small><small>Help</small></small></a></td> </tr> <tr align="center"> <td width="248" height="17" bgcolor="#C0C0C0" align="right"><p><fon t color="#000000"><small>Add Your test password:</small></font></td> <td height="17" width="123" bgcolor="#C0C0C0" align="left"><p><inpu t type="password"

name="password" size="8" tabindex="2" maxlength="8"></td> <td width="47" height="17" align="center" bgcolor="#C0C0C0"><a href="javascript:alert('The password must be between 4 and 8 charac ters long.')"><small><small>Help</small></small></a></td> </tr> <tr align="center"> <td width="248" height="1" bgcolor="#C0C0C0"></td> <td width="123" height="1" bgcolor="#C0C0C0"><p><input TYPE="button " NAME="FormsButton2" VALUE="save" ONCLICK="validateForm()" tabindex= "3"></td> <td width="47" height="1" align="center" bgcolor="#C0C0C0"><a href="javascript:alert('Click to save the details')"><small><small> Help</small></small></a></td> </tr> </table> </center></div></td> <td width="48%" align="center"><small>When you have added your own user name and password <a href="login.php">move onto the Login page to test it!</a></small></td> </tr> </table> </center></div> </form> <p align="center">&nbsp;</p> <p align="center"><a href="addausercode.php">Click here to view the PHP and JavaScript code</a> used for this page, or <a href=" http://www.thedemosite.co.uk/democode.zip">download the free zip here</a> </p> <?php include("footer.inc"); ?>

validateform.inc <?php // version 1.1 $qstr = "SELECT * from members where id = '1' "; $result = mysql_query($qstr); if (mysql_num_rows($result)) { $username = mysql_result($result,0, "username"); $password = mysql_result($result,0, "password"); echo "<b>The username:</b> $username<br>"; echo "<b>The password:</b> $password<br>"; } else echo "ERROR - unable to find current username and password!"; mysql_close(); ?>

config.php

<?php $mysql_database="a_db"; // change to your own $mysql_username="a_username"; // change to your own $mysql_password="a_password"; // change to your own $link = mysql_connect("localhost",$mysql_username,$mysql_password) or die ("Unable to connect to SQL server"); mysql_select_db($mysql_database,$link) or die ("Unable to select database"); ?>
getrecord.inc <?php // version 1.1 $qstr = "SELECT * from members where id = '1' "; $result = mysql_query($qstr); if (mysql_num_rows($result)) { $username = mysql_result($result,0, "username"); $password = mysql_result($result,0, "password"); echo "<b>The username:</b> $username<br>"; echo "<b>The password:</b> $password<br>"; } else echo "ERROR - unable to find current username and password!"; mysql_close(); ?>

savedata.php <?php include("config.php"); $qselect = "Select * from members where id = '1'"; $qadd = " INSERT INTO members (id, password, username) VALUES ('1', 'test', 'test') ";

$username = mysql_real_escape_string($_POST['username']); $password = mysql_real_escape_string($_POST['password']); if( ($username=="") OR ($password=="") ) { include("addauser.php"); exit;

} $qupdate = " UPDATE members SET password = '$password', username = '$username' WHERE id = '1'"; $result = mysql_query($qselect); // check the single record exists if (mysql_num_rows($result)) $result = mysql_query($qupdate); // update origi nal record else $result = mysql_query($qadd); // if not add an initial record if ($result) mysql_close(); else { echo "ERROR - unable to save new username and password!<br>"; $SQLError = "SQL ERROR: ".mysql_errno().". ".mysql_error()."<BR><BR>"; echo "$SQLError"; mysql_close(); } include("addauser.php"); ?>

Database MySQL admin table columns id, username, passcode. CREATE TABLE admin ( id INT PRIMARY KEY AUTO_INCREMENT, username VARCHAR(30) UNIQUE, passcode VARCHAR(30) ); Config.php Database configuration file.

<?php $mysql_hostname = "hostname"; $mysql_user = "username"; $mysql_password = "password"; $mysql_database = "database"; $bd = mysql_connect($mysql_hostname, $mysql_user, $mysql_password) or die("Opps some thing went wrong"); mysql_select_db($mysql_database, $bd) or die("Opps some thing went wrong"); ?>

Login.php Contains PHP and HTML code

>?php include("config.php"); session_start(); if($_SERVER["REQUEST_METHOD"] == "POST") { // username and password sent from Form $myusername=addslashes($_POST['username']); $mypassword=addslashes($_POST['password']); $sql="SELECT id FROM admin WHERE username='$myusername' and passcode='$mypassword'"; $result=mysql_query($sql); $row=mysql_fetch_array($result); $active=$row['active']; $count=mysql_num_rows($result); // If result matched $myusername and $mypassword, table row must be 1 row if($count==1) { session_register("myusername"); $_SESSION['login_user']=$myusername; header("location: welcome.php"); } else { $error="Your Login Name or Password is invalid"; } } ?> <form action="" method="post"> <label>UserName :</label> <input type="text" name="username"/><br /> <label>Password :</label> <input type="password" name="password"/><br/> <input type="submit" value=" Submit "/><br /> </form>
lock.php Session verification. If no session value page redirect to login.php

<?php

include('config.php'); session_start(); $user_check=$_SESSION['login_user']; $ses_sql=mysql_query("select username from admin where username='$user_check' "); $row=mysql_fetch_array($ses_sql); $login_session=$row['username']; if(!isset($login_session)) { header("Location: login.php"); } ?>
welcome.php

<?php include('lock.php'); ?> <body> <h1>Welcome <?php echo $login_session; ?></h1> </body>
logout.php SignOut Destroy the session value.

<?php session_start(); if(session_destroy()) { header("Location: login.php"); } ?>

You might also like