Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- <?php
- // First we execute our common code to connection to the database and start the session
- require("commonlogin.php");
- // At the top of the page we check to see whether the user is logged in or not
- if(empty($_SESSION['user']))
- {
- // If they are not, we redirect them to the login page.
- header("Location: login.php");
- // Remember that this die statement is absolutely critical. Without it,
- // people can view your members-only content without logging in.
- die("Redirecting to login.php");
- }
- // Everything below this point in the file is secured by the login system
- // We can display the user's username to them by reading it from the session array. Remember that because
- // a username is user submitted content we must use htmlentities on it before displaying it to the user.
- // This if statement checks to determine whether the edit form has been submitted
- // If it has, then the account updating code is run, otherwise the form is displayed
- if(!empty($_POST))
- {
- //START WEIRD EMAIL CHECKING
- // Make sure the user entered a valid E-Mail address
- if(!filter_var($_POST['email'], FILTER_VALIDATE_EMAIL))
- {
- die("Invalid E-Mail Address");
- }
- // If the user is changing their E-Mail address, we need to make sure that
- // the new value does not conflict with a value that is already in the system.
- // If the user is not changing their E-Mail address this check is not needed.
- if($_POST['email'] != $_SESSION['user']['email'])
- {
- // Define our SQL query
- $query = "
- SELECT
- 1
- FROM lfs
- WHERE
- email = :email
- ";
- // Define our query parameter values
- $query_params = array(
- ':email' => $_POST['email']
- );
- try
- {
- // Execute the query
- $stmt = $db->prepare($query);
- $result = $stmt->execute($query_params);
- }
- catch(PDOException $ex)
- {
- // Note: On a production website, you should not output $ex->getMessage().
- // It may provide an attacker with helpful information about your code.
- die("Failed to run query: " . $ex->getMessage());
- }
- // Retrieve results (if any)
- $row = $stmt->fetch();
- if($row)
- {
- die("This E-Mail address is already in use");
- }
- }
- //END WEIRD EMAIL CHECKING
- // If the user entered a new password, we need to hash it and generate a fresh salt
- // for good measure.
- if(!empty($_POST['password']))
- {
- $salt = dechex(mt_rand(0, 2147483647)) . dechex(mt_rand(0, 2147483647));
- $password = hash('sha256', $_POST['password'] . $salt);
- for($round = 0; $round < 65536; $round++)
- {
- $password = hash('sha256', $password . $salt);
- }
- }
- else
- {
- // If the user did not enter a new password we will not update their old one.
- $password = null;
- $salt = null;
- }
- // Initial query parameter values
- $query_params = array(
- ':user_id' => $_SESSION['user']['id'],
- ':email' => $_POST['email'],
- );
- if($_POST['scrim'] !== null)
- {
- $query_params[':scrim'] = $_POST['scrim'];
- }
- // If the user is changing their password, then we need parameter values
- // for the new password hash and salt too.
- if($password !== null)
- {
- $query_params[':password'] = $password;
- $query_params[':salt'] = $salt;
- }
- // Note how this is only first half of the necessary update query. We will dynamically
- // construct the rest of it depending on whether or not the user is changing
- // their password.
- $query = "
- UPDATE lfs
- SET
- email = :email ,
- ";
- if($_POST['scrim'] !== null)
- {
- $query .= "
- scrim = :scrim ,
- ";
- }
- // If the user is changing their password, then we extend the SQL query
- // to include the password and salt columns and parameter tokens too.
- if($password !== null)
- {
- $query .= "
- password = :password ,
- salt = :salt ,
- ";
- }
- // Finally we finish the update query by specifying that we only wish
- // to update the one record with for the current user.
- $query .= "
- WHERE
- id = :user_id ;
- ";
- try
- {
- // Execute the query
- $stmt = $db->prepare($query);
- $result = $stmt->execute($query_params);
- }
- catch(PDOException $ex)
- {
- // Note: On a production website, you should not output $ex->getMessage().
- // It may provide an attacker with helpful information about your code.
- die("Failed to run query: " . $ex->getMessage());
- }
- // Now that the user's E-Mail address has changed, the data stored in the $_SESSION
- // array is stale; we need to update it so that it is accurate.
- $_SESSION['user']['email'] = $_POST['email'];
- // This redirects the user back to the members-only page after they register
- header("Location: private.php");
- // Calling die or exit after performing a redirect using the header function
- // is critical. The rest of your PHP script will continue to execute and
- // will be sent to the user if you do not die or exit.
- die("Redirecting to private.php");
- }
- ?>
- <!DOCTYPE html>
- <html lang="en">
- <head>
- <meta charset="utf-8">
- <meta name="viewport" content="width=device-width, initial-scale=1.0">
- <meta name="description" content="">
- <meta name="author" content="">
- <title>ScrimNow - Account</title>
- <!-- Bootstrap core CSS -->
- <link href="css/bootstrap.css" rel="stylesheet">
- <link href="css/stylesheet.css" rel="stylesheet">
- <!-- Add custom CSS here -->
- <style>
- body {
- margin-top: 60px;
- }
- </style>
- </head>
- <body>
- <nav class="navbar navbar-default navbar-fixed-top" role="navigation">
- <div class="container">
- <div class="navbar-header">
- <button type="button" class="navbar-toggle" data-toggle="collapse" data-target=".navbar-ex1-collapse">
- <span class="sr-only">Toggle navigation</span>
- <span class="icon-bar"></span>
- <span class="icon-bar"></span>
- <span class="icon-bar"></span>
- </button>
- <a class="navbar-brand" href="index.php">ScrimNow</a>
- </div>
- <!-- Collect the nav links, forms, and other content for toggling -->
- <div class="collapse navbar-collapse navbar-ex1-collapse">
- <ul class="nav navbar-nav">
- <li><a href="index.php">Home</a>
- </li>
- <li><a href="lfs.php">LFS</a>
- </li>
- </ul>
- <ul class="nav navbar-nav navbar-right">
- <li class="dropdown">
- <a href="#" class="dropdown-toggle" data-toggle="dropdown"><?
- if(empty($_SESSION['user']))
- {
- // If they are not, we redirect them to the login page.
- echo"Account";
- // Remember that this die statement is absolutely critical. Without it,
- // people can view your members-only content without logging in.
- die("Account");
- }else{
- echo htmlentities($_SESSION['user']['username'], ENT_QUOTES, 'UTF-8');
- } ?> <span class="caret"></span></a>
- <ul class="dropdown-menu" role="menu">
- <?
- if(empty($_SESSION['user']))
- {
- // If they are not, we redirect them to the login page.
- echo"<li><a href='login.php'>Login</a></li>";
- // Remember that this die statement is absolutely critical. Without it,
- // people can view your members-only content without logging in.
- die("<li><a href='login.php'>Login</a></li>");
- }else{
- echo "<li><a href='#'>Edit Account</a></li>";
- } ?>
- <?
- if(empty($_SESSION['user']))
- {
- // If they are not, we redirect them to the login page.
- echo"<li><a href='register.php'>Register</a></li>";
- // Remember that this die statement is absolutely critical. Without it,
- // people can view your members-only content without logging in.
- die("<li><a href='register.php'>Register</a></li>");
- }else{
- echo "<li><a href='logout.php'>Logout</a></li>";
- } ?>
- </ul>
- </li>
- </ul>
- </div>
- <!-- /.navbar-collapse -->
- </div>
- <!-- /.container -->
- </nav>
- <div class="container">
- <div class="row">
- <div class="col-lg-12">
- <div class="jumbotron">
- <h1>Edit Account</h1>
- <p>Change your account details here!</p>
- <form action="private.php" method="post" role="form">
- <div class="form-group">
- <label for="exampleInputEmail1">Email address</label>
- <input type="email" name="email" value="<?php echo htmlentities($_SESSION['user']['email'], ENT_QUOTES, 'UTF-8'); ?>" class="form-control" id="exampleInputEmail1" placeholder="Email Address">
- </div>
- <div class="form-group">
- <label for="exampleInputPassword1">Password</label>
- <input name="password" type="password" class="form-control" id="exampleInputPassword1" placeholder="Password">
- <small>*Leave blank if you don't want to change it</SMALL>
- </div>
- <div class="form-group">
- <label for="exampleInputPassword1">Scrim</label>
- <input name="scrim" class="form-control" id="exampleInputPassword1" placeholder="0/1">
- </div>
- <button type="submit" class="btn btn-default">Update Account</button>
- </form>
- </div>
- </div>
- </div>
- </div>
- <!-- /.container -->
- <!-- JavaScript -->
- <script src="js/jquery-2.1.1.min.js"></script>
- <script src="js/bootstrap.js"></script>
- </body>
- </html>
Advertisement
Add Comment
Please, Sign In to add comment