Добавих емайл и скайп в една логин система. Регистрирах се, но не можах да си вляза в регистрацията.
Ето файловете:
error.php
login.php
logout.php
register.php
main.php
+1
Ето файловете:
error.php
<?php
session_start();
?>
<div>
<?php
if(isset($_GET["user_exists"])) {
echo "- This user already exists. Choose another username.<br />";
}
if(isset($_GET["email_exists"])) {
echo "- This email already exists. Choose another email.<br />";
}
if(isset($_GET["password_wrong_repeated"])) {
echo "- The password was not repeated correctly.<br />";
}
if(isset($_GET["password_not_written"])) {
echo "- You didnt write a password.<br />";
}
if(isset($_GET["user_doesnt_exist"])) {
echo "- This user does not exist.<br />";
}
if(isset($_GET["email_doesnt_exist"])) {
echo "- This email does not exist.<br />";
}
?>
<br />
<div>
<form action="login.php" method="post">
<div>
Username: <input type="text" name="username" class="text" />
</div><br>
<div>
Password: <input type="password" name="password" class="text" />
</div><br>
<div>
E-mail: <input type="text" name="email" class="text" />
</div><br>
<div>
Skype: <input type="text" name="skype" class="text" />
</div><br>
<div>
<input type="submit" value="Login" class="button" />
<input type="button" class="button" value="Register" onclick="window.location='registration.html'" />
</div>
</form>
</div>
</div>
login.php
<?php
session_start();
include("connection.php");
$username = $_POST["username"];
$password = $_POST["password"];
$email = $_POST["email"];
$skype = $_POST["skype"];
if(strlen($username) > 0 && strlen($password) > 0 && strlen($email) > 0) {
$query = mysql_query("SELECT * FROM users WHERE username = '$username'");
// We are checking does username exists
if(mysql_num_rows($query) == 0) {
header("Location: error.php?user_doesnt_exist");
}
// If user exists we continue
if($row = mysql_fetch_array($query)) {
$password_registered = $row["password"];
// If the password was entered correctly
if(md5($password) == $password_registered) {
$_SESSION["username"] = $username;
$_SESSION["password"] = $password;
$_SESSION["loggedin"] = true;
header("Location: index.html");
}
}
} else {
header("Location: index.html");
}
?>
logout.php
<?php
session_start();
$_SESSION["username"] = "";
$_SESSION["password"] = "";
$_SESSION["email"] = "";
$_SESSION["loggedin"] = false;
header("Location: index.html");
?>
register.php
<?php
include("connection.php");
session_start();
$action = $_GET["action"];
if($action == 2) {
$username = $_POST["username"];
$password = $_POST["password"];
$password2 = $_POST["password2"];
$email = $_POST["email"];
$skype = $_POST["skype"];
function check_username($username) {
$query = "SELECT * FROM users WHERE username = '$username'";
$query = mysql_query($query);
$num_rows = mysql_num_rows($query);
if($num_rows == 0) {
return 0;
} else {
return 1;
}
}
// Check if username exists
if(check_username($username) == 1) {
header("Location: error.php?user_exists");
}
// If username doesnt exist we continue
else {
if($password == $password2 && strlen($password) > 0) {
// If the password is repeated correctly
$md5_password = md5($password);
$query = "INSERT INTO users VALUES('$username', '$md5_password')";
// Adding username and password to the DB
mysql_query($query);
// Redirecting to the Index. User can login.
header("Location: index.html");
} else {
// If the password is not repeated correctly
header("Location: error.php?password_wrong_repeated");
}
}
} else {
?>
<div>
<form action="?action=2" method="post">
<div>
Username: <input type="text" name="username" class="text" />
</div><br>
<div>
Password: <input type="password" name="password" class="text" />
</div><br>
<div>
Repeat: <input type="password" name="password2" class="text" />
</div><br>
<div>
E-mail: <input type="text" name="email" class="text" />
</div><br>
<div>
Skype: <input type="text" name="skype" class="text" />
</div><br>
<div>
<input type="submit" value="Register" class="button" />
</div>
</form>
</div>
</center>
</body>
</html>
<?php
}
?>
main.php
<?php
session_start();
?>
<div>
<?php
if($_SESSION["loggedin"]) {
echo "Hello ".$_SESSION["username"]."<br />";
echo '<a href="logout.html">Logout</a>';
} else {
?>
<form action="login.php" method="post">
<div>
Username: <input type="text" name="username" class="text" />
</div><br>
<div>
Password: <input type="password" name="password" class="text" />
</div><br>
<div>
<input type="submit" value="Login" class="button" />
<input type="button" class="button" value="Register" onclick="window.location='registration.html'" />
</div>
</form>
<?php } ?>
</div>
</center>
</body>
</html>
+1