|
| Web Hosting Deals | Holiday Logo Design | Webcam Chat | Website Header Templates | Register domain | Search Engine Optimisation | Web Hosting |
|
|||||||
| PHP / Perl / Java / JavaScript / CGI Tutorials PHP / Perl / Java / JavaScript / CGI Tutorials Please do not use this Forum to advertise your site or to link to tutorials. |
![]() |
|
|
Submit Tools | LinkBack | Thread Tools | Display Modes |
|
||||
|
The CMS (#2) PHP
Ok so in the second part of this tutorial we are going to start creating the admin area.
Things we are going to use for this: Sessions If statements Functions MySQL database (but not yet) Great! Right then, let's get started with our simple login form.. login.php > Code:
<form action="check-user.php" method="post"> <b>Username</b>:<input type="text" name="username" size="20"><br> <b>Password</b>:<input type="password" name="pw" size="20"><br> <input type="submit" value="Login"></form> <form action="check-user.php" method="post"> - Tells the server once this script is posted take all the stuff inside the 2 <form> tags to the url check-user.php <input type="text" name="username" size="20"> - Text area to type your username into <input type="password" name="pw" size="20"> - Text area to type your password into <input type="submit" value="Login"> - Button you click to log in -_-_-_-_-_-_-_-_-_-_-_-_-_-_-_-_-_-_-_-_-_-_-_-_-_-_-_-_-_-_-_-_- Right then, so now we have that we need to build our check-user.php We are going to use sessions for this admin area, so that when a user leaves the website, they are automaticly logged off. PHP Code:
session_start(); - telling the server we are starting a session. include "../includes/connect.php"; - Including our connect.php that we created last tutorial. $check="SELECT * from users where username='$username' and pw='$pw'"; - Checking the username and password are correct $result= mysql_query($check) or die - Checks if the user & pass are correct if (mysql_num_rows($result) == 0) - Checks if it finds no combinations of the 2.. echo "<div align=center><b>Your login is wrong. Please click back and try again.</b></div>"; - Tells the user they entered in correct information } else - Otherwise: { $r=mysql_fetch_array($result); - Fetch information place into array $login_username=$r["username"]; - Gets their user and puts it into the variable $login_username session_register("login_username"); - Reigsters the username Header("Location: admin.php") - Redirects to our admin.php -_-_-_-_-_-_-_-_-_-_-_-_-_-_-_-_-_-_-_-_-_-_-_-_-_-_-_-_-_-_-_-_- So now what we need to do, is build the database, that is the backend of the what we were just checking. Open up PHP my admin or any other database tool and place this code into your SQL window CREATE TABLE users( id INT NOT NULL AUTO_INCREMENT, PRIMARY KEY(id), username VARCHAR(12), pw VARCHAR(32)) What we did: Created a table in the database, called users Row1: ID, Auto increment Primary Key is ID Username, max charicters 12 pw max charicters 32 Or alteranatively, if you don't have access too php my admin or an alternative, then create a file called install.php in your install directory and put this code in it: PHP Code:
That will do the same thing, just in PHP. -_-_-_-_-_-_-_-_-_-_-_-_-_-_-_-_-_-_-_-_-_-_-_-_-_-_-_-_-_-_-_-_- Creating our admin.php So we need to first make sure that the user is logged in, if they are not then we need to redirect them to the login page. We do that with the following code: session_start(); if($login_username=="") { Header("Location: login.php"); So if $login_username dosen't have anything in, then it redirects them to login.php PHP Code:
Just contains some links to the different pages that we will create. Note we will add more links as we add more features. End of tutorial #2 Coming up next, we are going to do the news section. Base
__________________
http://www.pointserv.co.uk/gfx/deal_..._pointserv.png PointServ.co.uk - One of the UK's cheapest webhosting companies. Last edited by Base; 03-31-2007 at 09:32 PM. Reason: Took the variable $seperateconnect out, as it was not needed |
|
||||
|
The variable $seperateconnect should be removed from the connect.php & from the check-user.php files.
This was used in error. Many thanks Thomas Gray
__________________
http://www.pointserv.co.uk/gfx/deal_..._pointserv.png PointServ.co.uk - One of the UK's cheapest webhosting companies. |
|
||||
|
ok now i got a small problem i get an error saying
Code:
No database selected
__________________
OUTLAW-WEB.NET |
|
||||
|
lol, you are my un-official bug tester!
Now this works perfectly for me, but again its you that has the problem. So try putting these lines into your install.php **NOTE THESE LINES HAVE TO BE JUST AFTER THE <? OF THE INSTALL.PHP!!!! ** PHP Code:
If it still failes, then get back to me via PM, rather than spamming this thread. Cheers Base
__________________
http://www.pointserv.co.uk/gfx/deal_..._pointserv.png PointServ.co.uk - One of the UK's cheapest webhosting companies. |
![]() |
| Bookmarks |
| Thread Tools | |
| Display Modes | |
|
|