Jump to content
GIGN Forum

Problema Ar Php


Dark13
 Share

Recommended Posts

Nemts no saita: http://www.techtuts.com/forums/index.php?showtopic=48

Database Setup

We will use a database to store all the user information in. Once you have created a database, go to phpmyadmin and run this query:

CREATE TABLE `users` (
`id` int(11) NOT NULL auto_increment,
`username` varchar(30) NOT NULL default '',
`password` varchar(255) NOT NULL default '',
`email` varchar(40) NOT NULL default '',
`msn` varchar(250) NOT NULL default 'Not Specified',
`aim` varchar(250) NOT NULL default 'Not Specified',
`location` varchar(36) NOT NULL default 'Not Specified',
PRIMARY KEY (`id`)
) TYPE=MyISAM;

Connecting to the database

Now we need a file to connect to the database with.

<?
  ob_start(); // allows you to use cookies
  $conn = mysql_connect("localhost","DATABASE USERNAME","DATABASE PASSWORD");
  mysql_select_db(DATABASE NAME) or die(mysql_error());
  //fill in the above lines where there are capital letters.
  $logged = MYSQL_QUERY("SELECT * from users WHERE id='$_COOKIE[id]'");
  $logged = mysql_fetch_array($logged);
  //the above lines get the user's information from the database.
?>

Save that file as config.php.

Setup

You will need to add some code to the TOP of every page you want to use the login system on. (not including the pages below). This is the code:

<?
ob_start();
include("config.php");
?>

We are now ready to start coding the pages.

Register.php

Register.php will allow users to register. Be sure to read the comments.

<?php
ob_start();
// allows you to use cookies
include("config.php");
//gets the config page
if ($_POST[register]) {
// the above line checks to see if the html form has been submitted
$username = $_POST[username];
$password = $_POST[pass];
$cpassword = $_POST[cpass];
$email = $_POST[emai1];
//the above lines set variables with the user submitted information
if($username==NULL|$password==NULL|$cpassword==NULL|$email==NULL) {
//checks to make sure no fields were left blank
echo "A field was left blank.";
}else{
//none were left blank!  We continue...
if($password != $cpassword) {
// the passwords are not the same!  
echo "Passwords do not match";
}else{
// the passwords are the same!  we continue...
$password = md5($password);
// encrypts the password
$checkname = mysql_query("SELECT username FROM users WHERE username='$username'");
$checkname= mysql_num_rows($checkname);
$checkemail = mysql_query("SELECT email FROM users WHERE email='$email'");
$checkemail = mysql_num_rows($checkemail);
if ($checkemail>0|$checkname>0) {
// oops...someone has already registered with that username or email!
echo "The username or email is already in use";
}else{
// noone is using that email or username!  We continue...
$username = htmlspecialchars($username);
$password = htmlspecialchars($password);
$email = htmlspecialchars($email);
// the above lines make it so that there is no html in the user submitted information.
//Everything seems good, lets insert.
$query = mysql_query("INSERT INTO users (username, password, email) VALUES('$username','$password','$email')");
// inserts the information into the database.
echo "You have successfully registered!";
}
}
}
}
else
{
// the form has not been submitted...so now we display it.
echo ("
<center>
<form method=\"POST\">
Username: <input type=\"text\" size=\"15\" maxlength=\"25\" name=\"username\"><br />
Password: <input type=\"password\" size=\"15\" maxlength=\"25\" name=\"pass\"><br />
Confirm Password: <input type=\"password\" size=\"15\" maxlength=\"25\" name=\"cpass\"><br />
Email: <input type=\"text\" size=\"15\" maxlength=\"25\" name=\"emai1\"><br />
<input name=\"register\" type=\"submit\" value=\"Register\">
</form>
</center>
");
}
?>

Login.php

Now that people can register, we need to let them login!. You can put this code in a block on your site, or put it in a blank page.

<?
oB_start();
// allows you to use cookies.
include("config.php");
if (!$logged[username])
{
if (!$_POST[login])
{
echo("
<center><form method=\"POST\">
<table>
<tr>
<td align=\"right\">
Username: <input type=\"text\" size=\"15\" maxlength=\"25\" name=\"username\">
</td>
</tr>
<tr>
<td align=\"right\">
Password: <input type=\"password\" size=\"15\" maxlength=\"25\" name=\"password\">
</td></tr><tr>
<td align=\"center\">
<input type=\"submit\" name=\"login\" value=\"Login\">
</td></tr><tr>
<td align=\"center\">
<a href=\"register.php\">Register Here</a>
</td></tr></table></form></center>");
}
if ($_POST[login]) {
// the form has been submitted.  We continue...
$username=$_POST['username'];
$password = md5($_POST[password]);
// the above lines set variables with the submitted information.  
$info = mysql_query("SELECT * FROM users WHERE username = '$username'") or die(mysql_error());
$data = mysql_fetch_array($info);
if($data[password] != $password) {
// the password was not the user's password!
echo "Incorrect username or password!";
}else{
// the password was right!
$query = mysql_query("SELECT * FROM users WHERE username = '$username'") or die(mysql_error());
$user = mysql_fetch_array($query);
// gets the user's information
setcookie("id", $user[id],time()+(60*60*24*5), "/", "");
setcookie("pass", $user[password],time()+(60*60*24*5), "/", "");
// the above lines set 2 cookies. 1 with the user's id and another with his/her password.  
echo ("<meta http-equiv=\"Refresh\" content=\"0; URL=http://yoursite.com\"/>Thank You! You will be redirected");
// modify the above line...add in your site url instead of yoursite.com
}
}
}
else
{
// we now display the user controls.
echo ("<center>Welcome <b>$logged[username]</b><br /></center>
- <a href=\"editprofile.php\">Edit Profile</a><br />
- <a href=\"members.php\">Member List</a><br />
- <a href=\"logout.php\">Logout</a>");
}
?>

Edit Profile

This page will allow users to update their information. Save it as editprofile.php.

<?
ob_start();
include("config.php");
if ($logged[username])
{
// the user is logged in!  We continue...
if (!$_POST[update])
{
// the form hasn't been submitted.  We continue...
$profile = mysql_query("SELECT * from users where username = '$logged[username]'");
$profile = mysql_fetch_array($profile);
// the above lines get the information so that it can be displayed in the html form.
echo("
<center><form method=\"POST\">
<table width=\"100%\">
<tr>
<td align=\"right\" width=\"25%\">
Location 
</td>
<td align=\"left\">
<input type=\"text\" size=\"25\" maxlength=\"25\" name=\"locate\" value=\"$profile[location]\"></td>
</tr>
<tr>
<td align=\"right\" width=\"25%\">
MSN Messenger 
</td>
<td align=\"left\">
<input size=\"25\" name=\"msn\" value=\"$profile[msn]\"></td>
</tr>
<tr>
<td align=\"right\" width=\"25%\">
AOL Messenger</td>
<td align=\"left\">
<input size=\"25\" name=\"aim\"  value=\"$profile[aim]\"></td>
</tr>
<tr>
<td align=\"right\" width=\"25%\">
Email Address</td>
<td align=\"left\">
<input size=\"25\"  name=\"email\" value=\"$profile[email]\"></td>
</tr>
<tr>
<td align=\"center\">
</td>
<td align=\"left\">
<input type=\"submit\" name=\"update\" value=\"Update\"></td>
</tr>
</table>
</form>
</center>");
}
else
{
$email = htmlspecialchars($_POST[email]);
$aim = htmlspecialchars($_POST[aim]);
$msn = htmlspecialchars($_POST[msn]);
$locate = htmlspecialchars($_POST[locate]);
// the above lines get rid of all html.
echo ("Your profile has been updated!");
$update = mysql_query("Update users set email = '$email', 
msn = '$msn', aim = '$aim', location = '$locate' where username = '$logged[username]'");
// updates the information in the database.
}
}
else
{
// They aren't logged in!
echo ("<a href=\"login.php\">You must login</a>");
}
?>

Display Users Information

Now we need to have a page where members can view other member's profiles.

Use this code, and save this as members.php.

<?
ob_start();
include("config.php");
if (!$_GET[user])
{
$getuser = mysql_query("SELECT * from users order by id asc");
while ($user = mysql_fetch_array($getuser))
{
// gets all the users information. 
echo ("<a href=\"members.php?user=$user[username]\">$user[username]</a><br />\n");
// links to a page to view the user's profile.
}
}
ELSE
{
$getuser = mysql_query("SELECT * from users where username = '$_GET[user]'");
$usernum = mysql_num_rows($getuser);
if ($usernum == 0)
{
echo ("User Not Found");
}
else
{
$profile = mysql_fetch_array($getuser);
echo ("<center><b>$profile[username]'s Profile:</b><br /></center>
MSN Messenger: $profile[msn]<br />
AIM Messebger: $profile[aim]<br />
Location: $profile[location]<br />
Email: $profile[email]");
// in the above code, we display the user's information.
}
}
?>

Logout.php

This page logs the user out. It just updates the cookie with false information.

<?
ob_start();
setcookie("id", 2132421,time()+(60*60*24*5), "/", ""); 
setcookie("pass", loggedout,time()+(60*60*24*5), "/", "");
echo ("You are now logged out!");
?>

That concludes this huge tutorial. If you need any help please leave a comment below.

Showing the login box in a content box

Add this code where you want the login system:

<?
  include("login.php");
?>

Members Only Pages

Use this code for a members only page.

<?
  ob_start();
  include("config.php");
  if ($logged[username])
  {
     echo("You are logged in");
  }
  else
  {
     echo("You are not logged in");
  }
?>

Domaju ka palidzes!

Sita laba pamaciba!

Pats kautkad saksu veidot savu WEB nevis php-Fusion un izmantosu kautko no sita!

Edited by Grow3r
Link to comment
Share on other sites

Liels tev paldies Grow3r smile.gif

un velviens jautajumins ka var html lapa ieklaut php kodu??

1kārt

pārsauc savu dokumentu uz tavsnosaukums.php, nevis.html

un kur tu gribi iebāzt savu php scriptu raksti

<?php
tavs_kods;
?>

protams var jau ari <?php tavs_kods; ?>

Edited by darker
Link to comment
Share on other sites

jo uztaisīt to var tikai ilgā laikā un ar labu skilu programmēšanā (PHP / HTML etc.)

Vienkārši logins ar pāris huiņām nav pat pusstundas jautājums. wink.gif

A PHP kodam par HTML nospļauties. Principā jams ļoti labi strādā arī nesaistīti ar HTML vai internetu vispār. Kaut kas jāizvada ( ar to pašu echo ) ir tad, ja tu gribi, lai kaut ko noteiktu parāda pārlūkā.

Link to comment
Share on other sites

Guest
This topic is now closed to further replies.
 Share

×
×
  • Create New...