html / xhtml, css, php, mysql, javascript, asp, seo, web management, semantics, accessibility and more.
Have you ever wanted to add your own admin section to your website that only you can get to, well with this script now you can. All you need is PHP and MySQL.
In category php.
Posted by Joseph Skidmore on 10.09.05.
Have you ever wanted to add your own admin section to your website that only you can get to, well with this script now you can. All you need is PHP and MySQL.
First thing your going to need to do is create your database, call it 'login'. Next thing is to go into PHPMyAdmin and add the following query to your database:
Done that? good, next thing we need to do is create our login form. The one I used was:
<form method="post" name="login" action="process.php">
<p>Username : <input type="text" name="username" /></p>
<p>Password : <input type="password" name="password" /></p>
<p><input type="submit" name="submit" value="Submit" /></p>
</form>
<?php
if (isset($_GET['error'])) {
echo 'Invalid login data supplied. Please try again.';
}
?>
Now, in this code you can see that when the submit button gets pressed it takes us to our process.php page, this is for processing the login and making sure that the user actually exists. The code at the bottom of this script:
Just means, if there is an error write on the page 'Invalid login data supplied. Please try again.. on the page. The next part of the code is process.php.
Something you may not have seen before is session_start();. Here we are using sessions, this is the code to declare that we are doing so. The code that you may already know, have got php comments next to them, the rest I will explain.
Select the fields 'user' and 'pass' from the table 'login' and give the data inside the user field the variable name $username and the password field the variable name $password. The second line of the code is the whole query which we are giving a variable name $result.
Here we have, the if/else statement, if our variables from the form match the data inside the database then forward you to http://www.domain.com/admin.php. If they do not match then go back to our login form with an error.
Still with me? good, now lets move onto our admin section.
You remember those sessions I was telling you about, this is where they come into play, the code we have in basic english translates to:
Easy huh? The next part would be our logout script, which is very simple. Sessions are stored in the users browser and stay there until they logout or close the browser window, so all we need to do on the logout script is destroy these variables.
Not that hard was it? If you have any questions or problems don't hesitate to contact me using the feedback form or head over to Weborum to contact me or any of the other talented members.
If you would like to download the pre-written admin script you can do so here: Pre Written Admin Section Script (Downloaded Times)