Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- <?php
- $connection = mysqli_connect('localhost','root','');
- if(!$connection){
- echo "faild to connect to db" . die(mysqli_error($connection));
- }
- $dbselect = mysqli_select_db($connection,'new');
- if(!$dbselect){
- echo "faild to select db" . die(mysqli_error($connection));
- }
- ?>
- <?php
- require_once('connect.php');
- if(isset($_POST) & !empty($_POST)){
- $username = $_POST['username'];
- $email = $_POST['email'];
- $password = $_POST['password'];
- $sql = "INSERT INTO `user` (username,email,password,uid) VALUES ('$username','$email','$password',1)";
- $result = mysqli_query($connection, $sql);
- if ($result){
- $smsg = "user registered successfully";
- }else{
- $fmsg = "user registration failed";
- }
- }
- ?>
- <!doctype html>
- <html lang="en">
- <head>
- <title>register user with php</title>
- <link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/css/bootstrap.min.css" >
- <link rel="stylesheet" href="styles.css" >
- <script src="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/js/bootstrap.min.js"></script>
- </head>
- <body>
- <div class="container">
- <?php if(isset($smsg)){ ?><div class="alert alert-success" role="alert"> <?php echo $smsg; ?> </div><?php } ?>
- <?php if(isset($fmsg)){ ?><div class="alert alert-danger" role="alert"> <?php echo $fmsg; ?> </div><?php } ?>
- <form class="form-signin" method="POST">
- <h2 class="form-signin-heading">Please Register</h2>
- <div class="input-group">
- <span class="input-group-addon" id="basic-addon1">username:</span>
- <input type="text" name="username" class="form-control" placeholder="Username" required>
- </div>
- <label for="inputEmail" class="sr-only">Email address</label>
- <input type="email" name="email" id="inputEmail" class="form-control" placeholder="Email address" required autofocus>
- <label for="inputPassword" class="sr-only">Password</label>
- <input type="password" name="password" id="inputPassword" class="form-control" placeholder="Password" required>
- <button class="btn btn-lg btn-primary btn-block" type="submit">Register</button>
- <a class="btn btn-lg btn-primary btn-block" href="login.php">Login</a>
- </form>
- </div>
- </body>
- </html>
- <html>
- <head>
- <title>User Login Using PHP & MySQL</title>
- <link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/css/bootstrap.min.css" >
- <link rel="stylesheet" href="style.css" >
- <script src="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/js/bootstrap.min.js"></script>
- </head>
- <body>
- <?php //Start the Session
- session_start();
- require('connect.php');
- //3. If the form is submitted or not.
- //3.1 If the form is submitted
- if (isset($_POST['username']) and isset($_POST['password'])){
- //3.1.1 Assigning posted values to variables.
- $username = $_POST['username'];
- $password = $_POST['password'];
- //3.1.2 Checking the values are existing in the database or not
- $query = "SELECT * FROM `user` WHERE username='$username' and password='$password'";
- $result = mysqli_query($connection, $query) or die(mysqli_error($connection));
- $count = mysqli_num_rows($result);
- //3.1.2 If the posted values are equal to the database values, then session will be created for the user.
- if ($count == 1){
- $_SESSION['username'] = $username;
- }else{
- //3.1.3 If the login credentials doesn't match, he will be shown with an error message.
- $fmsg = "Invalid Login Credentials.";
- }
- }
- //3.1.4 if the user is logged in Greets the user with message
- if (isset($_SESSION['username'])){
- $username = $_SESSION['username'];?>
- <?php
- require_once('connect.php');
- if(isset($_POST) & !empty($_POST)){
- $username1 = $_POST['username1'];
- $email1 = $_POST['email1'];
- $password1 = $_POST['password1'];
- $sql3 = "SELECT id, username, uid FROM user where username='$username'";
- $result3 = $connection->query($sql3);
- if ($result3->num_rows > 0) {
- // output data of each row
- while($row = $result3->fetch_assoc()) {
- $uidreg = $row['id'];
- }
- } else {
- echo "0 results3";
- }
- $sql2 = "INSERT INTO `user` (username,email,password,uid) VALUES ('$username1','$email1','$password1','$uidreg')";
- $result2 = mysqli_query($connection, $sql2);
- if ($result2){
- echo "user registered successfully";
- if (!mkdir("user $username1", 0777, true)) {
- die('Failed to create folders...');}
- }else{
- echo "register failed";
- }
- }
- ?>
- <div id='box'>
- <form class="form-signin" method="POST">
- <?php echo "<h2 class='form-signin-heading'> Hello $username Please Register</h2>"?>
- <div class="input-group">
- <span class="input-group-addon" id="basic-addon1">username:</span>
- <input type="text" name="username1" class="form-control" placeholder="Username" required>
- </div>
- <label for="inputEmail" class="sr-only">Email address</label>
- <input type="email" name="email1" id="inputEmail" class="form-control" placeholder="Email address" required autofocus>
- <label for="inputPassword" class="sr-only">Password</label>
- <input type="password" name="password1" id="inputPassword" class="form-control" placeholder="Password" required>
- <button class="btn btn-lg btn-primary btn-block" type="submit" name="regSubmit">Register</button>
- <a class="btn btn-lg btn-primary btn-block" href="user-login.php">Login</a>
- </form>
- <br><?php echo "<a href='logout.php'>Logout</a>"; ?>
- </div>
- <?php }else{
- //3.2 When the user visits the page first time, simple login form will be displayed.
- ?>
- <div class="container">
- <form class="form-signin" method="POST">
- <?php if(isset($fmsg)){ ?><div class="alert alert-danger" role="alert"> <?php echo $fmsg; ?> </div><?php } ?>
- <h2 class="form-signin-heading">Please Login</h2>
- <div class="input-group">
- <span class="input-group-addon" id="basic-addon1">username:</span>
- <input type="text" name="username" class="form-control" placeholder="Username" required>
- </div>
- <div class="input-group">
- <label for="inputPassword" class="sr-only">Password</label>
- <span class="input-group-addon" id="basic-addon1">Password:</span>
- <input type="password" name="password" id="inputPassword" class="form-control" placeholder="Password" required>
- </div>
- <button class="btn btn-lg btn-primary btn-block" type="submit" name="loginSubmit">Login</button>
- </form>
- </div>
- </body>
- </html>
- <?php } ?>
- <?php
- session_start();
- session_destroy();
- header('Location: login.php');
- ?>
- <html>
- <head>
- <title>User Login Using PHP & MySQL</title>
- <link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/css/bootstrap.min.css" >
- <link rel="stylesheet" href="style.css" >
- <script src="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/js/bootstrap.min.js"></script>
- </head>
- <body>
- <?php //Start the Session
- session_start();
- require('connect.php');
- //3. If the form is submitted or not.
- //3.1 If the form is submitted
- if (isset($_POST['username2']) and isset($_POST['password2'])){
- //3.1.1 Assigning posted values to variables.
- $username2 = $_POST['username2'];
- $password2 = $_POST['password2'];
- //3.1.2 Checking the values are existing in the database or not
- $query = "SELECT * FROM `user` WHERE username='$username2' and password='$password2'";
- $result4 = mysqli_query($connection, $query) or die(mysqli_error($connection));
- $count4 = mysqli_num_rows($result4);
- //3.1.2 If the posted values are equal to the database values, then session will be created for the user.
- if ($count4 == 1){
- $_SESSION['username2'] = $username2;
- }else{
- //3.1.3 If the login credentials doesn't match, he will be shown with an error message.
- $fmsg = "Invalid Login Credentials.";
- }
- }
- //3.1.4 if the user is logged in Greets the user with message
- if (isset($_SESSION['username2'])){
- $username2 = $_SESSION['username2'];?>
- <?php
- $target_dir = "user $username2/";
- $target_file = $target_dir . basename($_FILES["fileToUpload"]["name"]);
- $uploadOk = 1;
- $imageFileType = strtolower(pathinfo($target_file,PATHINFO_EXTENSION));
- if(isset($_POST["submit"])) {
- $check = getimagesize($_FILES["fileToUpload"]["tmp_name"]);
- if($check !== false) {
- echo "File is an image - " . $check["mime"] . ".";
- $uploadOk = 1;} else {
- echo "File is not an image.";
- $uploadOk = 0;}}
- if (file_exists($target_file)) {
- echo "Sorry, file already exists.";
- $uploadOk = 0;}
- if ($_FILES["fileToUpload"]["size"] > 500000) {
- echo "Sorry, your file is too large.";
- $uploadOk = 0;}
- if($imageFileType != "jpg" && $imageFileType != "png" && $imageFileType != "jpeg"
- && $imageFileType != "gif" ) {
- echo "Sorry, only JPG, JPEG, PNG & GIF files are allowed.";
- $uploadOk = 0;}
- if ($uploadOk == 0) {
- echo "Sorry, your file was not uploaded.";} else {
- if (move_uploaded_file($_FILES["fileToUpload"]["tmp_name"], $target_file)) {
- echo "The file ". basename( $_FILES["fileToUpload"]["name"]). " has been uploaded.";} else {
- echo "Sorry, there was an error uploading your file.";}}
- ?>
- <div id='box'>
- <?php echo "<h2> hi, $username2</h2>"; ?><br>
- <form method="post" enctype="multipart/form-data">
- Select image to upload:
- <input type="file" name="fileToUpload" id="fileToUpload">
- <input type="submit" value="Upload Image" name="submit">
- </form>
- <br><a href='user-logout.php'>Logout</a>"
- </div>
- <?php }else{
- //3.2 When the user visits the page first time, simple login form will be displayed.
- ?>
- <div class="container">
- <form class="form-signin" method="POST">
- <?php if(isset($fmsg)){ ?><div class="alert alert-danger" role="alert"> <?php echo $fmsg; ?> </div><?php } ?>
- <h2 class="form-signin-heading">Please Login</h2>
- <div class="input-group">
- <span class="input-group-addon" id="basic-addon1">username:</span>
- <input type="text" name="username2" class="form-control" placeholder="Username" required>
- </div>
- <div class="input-group">
- <label for="inputPassword" class="sr-only">Password</label>
- <span class="input-group-addon" id="basic-addon1">Password:</span>
- <input type="password" name="password2" id="inputPassword" class="form-control" placeholder="Password" required>
- </div>
- <button class="btn btn-lg btn-primary btn-block" type="submit" name="loginSubmit">Login</button>
- </form>
- </div>
- </body>
- </html>
- <?php } ?>
- <?php
- session_start();
- session_destroy();
- header('Location: user-login.php');
- ?>
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement