Sponsor-Board.de
Antwort schreiben  Thema schreiben 

Login System für Unreal [PHP7 & MySQL]

Verfasser Nachricht

Beiträge: 14
Bewertung: 0
Registriert seit: Oct 2016
Status: offline


Beitrag: #1
Login System für Unreal [PHP7 & MySQL]

Liebe Mitglieder des Sponsorboard,

ich habe mit mehren Freunden ein etwas größeres Projekt vor. Es geht um eine Spiele Entwicklung in Unreal Engine.

Nun habe ich mal angefangen und das Register / Login System mal fertig gestellt, mir geht es hier nur darum, damit erfahrene Leute den Code checken und mir vielleicht Tipps gebt wo ich noch nachbessern soll.

Ihr könnt über [Link: Registrierung erforderlich] die Seite checken, nach Bedarf kann ich auch gerne den Code mitteilen.

Vielleicht kann ja auch jemand checken ob ihr das Login System durchbrechen könnt Wink

Auf Feedback freue ich mich.

Liebe Grüße
Farnight1337


Freundliche Grüße

IT-Fr3akz.com
----------------------------------------------------------------------------------------------------------------

05.05.2018 12:57
 
Alle Beiträge dieses Benutzers finden Diese Nachricht in einer Antwort zitieren

Beiträge: 268
Bewertung: 11
Registriert seit: Sep 2013
Status: offline


Beitrag: #2
RE: Login System für Unreal [PHP7 & MySQL]

Der Code wäre selbstverständlich noch Praktisch Smile
Ist der Open Source oder nicht?


[Link: Registrierung erforderlich]

[Link: Registrierung erforderlich]

05.05.2018 13:21
 
Webseite des Benutzers besuchen Alle Beiträge dieses Benutzers finden Diese Nachricht in einer Antwort zitieren

Beiträge: 14
Bewertung: 0
Registriert seit: Oct 2016
Status: offline


Beitrag: #3
RE: Login System für Unreal [PHP7 & MySQL]

Hallo Superfarmer1995,

ja der Code ist selbstverständlich Open Source. Ich will einfach für ein "relativ" sicheres Login System aufbauen will.

Ich habe jeglich im Code die Zugangsdaten zum MySQL Server entfernt, da es sonst ein leichtes ist hier einfach einzusteigen ( wenn der Zugang frei wäre Wink )


Login.php

Code:
<?php
// Include config file
require_once 'config.php';

// Define variables and initialize with empty values
$username = $password = "";
$username_err = $password_err = "";

// Processing form data when form is submitted
if($_SERVER["REQUEST_METHOD"] == "POST"){

    // Check if username is empty
    if(empty(trim($_POST["username"]))){
        $username_err = 'Please enter username.';
    } else{
        $username = trim($_POST["username"]);
    }
    
    // Check if password is empty
    if(empty(trim($_POST['password']))){
        $password_err = 'Please enter your password.';
    } else{
        $password = trim($_POST['password']);
    }
    
    // Validate credentials
    if(empty($username_err) && empty($password_err)){
        // Prepare a select statement
        $sql = "SELECT username, password FROM users WHERE username = ?";
        
        if($stmt = mysqli_prepare($link, $sql)){
            // Bind variables to the prepared statement as parameters
            mysqli_stmt_bind_param($stmt, "s", $param_username);
            
            // Set parameters
            $param_username = $username;
            
            // Attempt to execute the prepared statement
            if(mysqli_stmt_execute($stmt)){
                // Store result
                mysqli_stmt_store_result($stmt);
                
                // Check if username exists, if yes then verify password
                if(mysqli_stmt_num_rows($stmt) == 1){                    
                    // Bind result variables
                    mysqli_stmt_bind_result($stmt, $username, $hashed_password);
                    if(mysqli_stmt_fetch($stmt)){
                        if(password_verify($password, $hashed_password)){
                            /* Password is correct, so start a new session and
                            save the username to the session */
                            session_start();
                            $_SESSION['username'] = $username;      
                            header("location: welcome.php");
                        } else{
                            // Display an error message if password is not valid
                            $password_err = 'The password you entered was not valid.';
                        }
                    }
                } else{
                    // Display an error message if username doesn't exist
                    $username_err = 'No account found with that username.';
                }
            } else{
                echo "Oops! Something went wrong. Please try again later.";
            }
        }
        
        // Close statement
        mysqli_stmt_close($stmt);
    }
    
    // Close connection
    mysqli_close($link);
}
?>

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>Login</title>
    <link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/css/bootstrap.css">
    <style type="text/css">
        body{ font: 14px sans-serif; }
        .wrapper{ width: 350px; padding: 20px; }
    </style>
</head>
<body>
    <div class="wrapper">
        <h2>Login</h2>
        <p>Please fill in your credentials to login.</p>
        <form action="<?php echo htmlspecialchars($_SERVER["PHP_SELF"]); ?>" method="post">
            <div class="form-group <?php echo (!empty($username_err)) ? 'has-error' : ''; ?>">
                <label>Username</label>
                <input type="text" name="username"class="form-control" value="<?php echo $username; ?>">
                <span class="help-block"><?php echo $username_err; ?></span>
            </div>    
            <div class="form-group <?php echo (!empty($password_err)) ? 'has-error' : ''; ?>">
                <label>Password</label>
                <input type="password" name="password" class="form-control">
                <span class="help-block"><?php echo $password_err; ?></span>
            </div>
            <div class="form-group">
                <input type="submit" class="btn btn-primary" value="Login">
            </div>
            <p>Don't have an account? <a href="register.php">Sign up now</a>.</p>
        </form>
    </div>    
</body>
</html>


index.php / Register

Code:
<?php
// Include config file
require_once 'config.php';

// Define variables and initialize with empty values
$username = $password = $confirm_password = "";
$username_err = $password_err = $confirm_password_err = "";

// Processing form data when form is submitted
if($_SERVER["REQUEST_METHOD"] == "POST"){

    // Validate username
    if(empty(trim($_POST["username"]))){
        $username_err = "Please enter a username.";
    } else{
        // Prepare a select statement
        $sql = "SELECT id FROM users WHERE username = ?";
        
        if($stmt = mysqli_prepare($link, $sql)){
            // Bind variables to the prepared statement as parameters
            mysqli_stmt_bind_param($stmt, "s", $param_username);
            
            // Set parameters
            $param_username = trim($_POST["username"]);
            
            // Attempt to execute the prepared statement
            if(mysqli_stmt_execute($stmt)){
                /* store result */
                mysqli_stmt_store_result($stmt);
                
                if(mysqli_stmt_num_rows($stmt) == 1){
                    $username_err = "This username is already taken.";
                } else{
                    $username = trim($_POST["username"]);
                }
            } else{
                echo "Oops! Something went wrong. Please try again later.";
            }
        }
        
        // Close statement
        mysqli_stmt_close($stmt);
    }
    
    // Validate password
    if(empty(trim($_POST['password']))){
        $password_err = "Please enter a password.";    
    } elseif(strlen(trim($_POST['password'])) < 6){
        $password_err = "Password must have atleast 6 characters.";
    } else{
        $password = trim($_POST['password']);
    }
    
    // Validate confirm password
    if(empty(trim($_POST["confirm_password"]))){
        $confirm_password_err = 'Please confirm password.';    
    } else{
        $confirm_password = trim($_POST['confirm_password']);
        if($password != $confirm_password){
            $confirm_password_err = 'Password did not match.';
        }
    }
    
    // Check input errors before inserting in database
    if(empty($username_err) && empty($password_err) && empty($confirm_password_err)){
        
        // Prepare an insert statement
        $sql = "INSERT INTO users (username, password) VALUES (?, ?)";
        
        if($stmt = mysqli_prepare($link, $sql)){
            // Bind variables to the prepared statement as parameters
            mysqli_stmt_bind_param($stmt, "ss", $param_username, $param_password);
            
            // Set parameters
            $param_username = $username;
            $param_password = password_hash($password, PASSWORD_DEFAULT); // Creates a password hash
            
            // Attempt to execute the prepared statement
            if(mysqli_stmt_execute($stmt)){
                // Redirect to login page
                header("location: login.php");
            } else{
                echo "Something went wrong. Please try again later.";
            }
        }
        
        // Close statement
        mysqli_stmt_close($stmt);
    }
    
    // Close connection
    mysqli_close($link);
}
?>

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>Sign Up</title>
    <link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/css/bootstrap.css">
    <style type="text/css">
        body{ font: 14px sans-serif; }
        .wrapper{ width: 350px; padding: 20px; }
    </style>
</head>
<body>
    <div class="wrapper">
        <h2>Sign Up</h2>
        <p>Please fill this form to create an account.</p>
        <form action="<?php echo htmlspecialchars($_SERVER["PHP_SELF"]); ?>" method="post">
            <div class="form-group <?php echo (!empty($username_err)) ? 'has-error' : ''; ?>">
                <label>Username</label>
                <input type="text" name="username"class="form-control" value="<?php echo $username; ?>">
                <span class="help-block"><?php echo $username_err; ?></span>
            </div>    
            <div class="form-group <?php echo (!empty($password_err)) ? 'has-error' : ''; ?>">
                <label>Password</label>
                <input type="password" name="password" class="form-control" value="<?php echo $password; ?>">
                <span class="help-block"><?php echo $password_err; ?></span>
            </div>
            <div class="form-group <?php echo (!empty($confirm_password_err)) ? 'has-error' : ''; ?>">
                <label>Confirm Password</label>
                <input type="password" name="confirm_password" class="form-control" value="<?php echo $confirm_password; ?>">
                <span class="help-block"><?php echo $confirm_password_err; ?></span>
            </div>
            <div class="form-group">
                <input type="submit" class="btn btn-primary" value="Submit">
                <input type="reset" class="btn btn-default" value="Reset">
            </div>
            <p>Already have an account? <a href="login.php">Login here</a>.</p>
        </form>
    </div>    
</body>
</html>


welcome.php

Code:
<?php
// Initialize the session
session_start();

// If session variable is not set it will redirect to login page
if(!isset($_SESSION['username']) || empty($_SESSION['username'])){
  header("location: login.php");
  exit;
}
?>

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>Welcome</title>
    <link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/css/bootstrap.css">
    <style type="text/css">
        body{ font: 14px sans-serif; text-align: center; }
    </style>
</head>
<body>
    <div class="page-header">
        <h1>Hi, <b><?php echo htmlspecialchars($_SESSION['username']); ?></b>. Welcome to our site.</h1>
    </div>
    <p><a href="logout.php" class="btn btn-danger">Sign Out of Your Account</a></p>
</body>
</html>


logout.php

Code:
<?php
// Initialize the session
session_start();

// Unset all of the session variables
$_SESSION = array();

// Destroy the session.
session_destroy();

// Redirect to login page
header("location: login.php");
exit;
?>


config.php

Code:
<?php
/* Database credentials. Assuming you are running MySQL
server with default setting (user 'root' with no password) */
define('DB_SERVER', '*****');
define('DB_USERNAME', '******');
define('DB_PASSWORD', '*********');
define('DB_NAME', '*********');

/* Attempt to connect to MySQL database */
$link = mysqli_connect(DB_SERVER, DB_USERNAME, DB_PASSWORD, DB_NAME);

// Check connection
if($link === false){
    die("ERROR: Could not connect. " . mysqli_connect_error());
}
?>



Wäre cool wenn du mir ein Feedback geben kannst.

Liebe Grüße
Farnight1337


Freundliche Grüße

IT-Fr3akz.com
----------------------------------------------------------------------------------------------------------------

05.05.2018 13:27
 
Alle Beiträge dieses Benutzers finden Diese Nachricht in einer Antwort zitieren

Beiträge: 343
Bewertung: 7
Registriert seit: May 2011
Status: offline


Beitrag: #4
RE: Login System für Unreal [PHP7 & MySQL]

sowas würde ich nie public ausgeben

die("ERROR: Could not connect. " . mysqli_connect_error());

sowas könnte pws un co ausgeben, wenn die mysql db mal offline ist, weil gerade ein update gemacht wird oder sonst was.

Guck dir auch mal [Link: Registrierung erforderlich] an da gibts paar vorschläge wie man, paar sachen umsetzen sollte, vom codestyle über autoloader bis hin zum caching.

Was ich an deiner Stelle aufjedenfall machen sollte wäre eher in Richtung MVC zu gehen, und als Start immer die index.php zu nehmen und wenigstens das layout immer wieder zu verwenden, das spart viel Zeit, in der programmierung und bei updates.
Auch mehr mit klassen zuarbeiten oder wenigstens Funtkionen, das wär schon mal ein Schritt.
Wenn du mit Klassen arbeitest könntest dir auch composer angucken mit dem PSR-4 Standard für die Klassennamen.

Das sind jetzt nur mal paar kleine bsp. man könnte auch sich ein Framework oder eine TemplateEngine als hilfe nutzen damit man besser die Struktur lernt, aber dafür bist du glaub noch zu weit am Anfang.


WebEntwickler mit den Schwerpunkten PHP-Backend, PHP-Frontend, jQuery, HTML, CSS

Github: [Link: Registrierung erforderlich]

05.05.2018 16:27
 
Webseite des Benutzers besuchen Alle Beiträge dieses Benutzers finden Diese Nachricht in einer Antwort zitieren

Beiträge: 14
Bewertung: 0
Registriert seit: Oct 2016
Status: offline


Beitrag: #5
RE: Login System für Unreal [PHP7 & MySQL]

Vielen Dank für dein Feedback Koksplfanze,

ich werde mich an der Umsetzung bemühen!

Vielen Dank


Freundliche Grüße

IT-Fr3akz.com
----------------------------------------------------------------------------------------------------------------

05.05.2018 16:45
 
Alle Beiträge dieses Benutzers finden Diese Nachricht in einer Antwort zitieren
Antwort schreiben  Thema schreiben 

Möglicherweise verwandte Themen...
Thema: Verfasser Antworten: Ansichten: Letzter Beitrag
  MySQL Code Fehler VarmintLP 6 2.319 06.07.2014 13:12
Letzter Beitrag: VarmintLP
  Arma III Mysql DB Neonwolve 2 2.138 16.03.2014 22:31
Letzter Beitrag: Neonwolve
  Metro Login Theme DebianDEV 15 3.234 18.08.2013 14:49
Letzter Beitrag: CreativeDEV.de
  Login Check Nilss 10 3.202 22.05.2013 11:56
Letzter Beitrag: push
  Login Box verschoben Hilfe Fredix 10 2.611 15.05.2012 21:27
Letzter Beitrag: Fredix

 Druckversion anzeigen
 Thema einem Freund senden
 Thema abonnieren
 Thema zu den Favoriten hinzufügen

Sponsor-Board.de

Community
Über uns
Partner
Powered by Mybb: Copyright 2002-2024 by MyBB Group - Deutsche-Übersetzung von Mybb.de
 
© 2007-2024 Sponsor-Board.de - Hosted by OVH

Willkommen auf SB!   Sie benötigen ein Sponsoring?   1. Anmelden   2. Sponsoring-Anfrage erstellen   3. Nachrichten von Sponsoren erhalten   Kostenlos!   Jetzt registrieren