Thread: Php coding help

    #1
  1. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Nov 2013
    Posts
    1
    Rep Power
    0

    Php coding help


    hya all ive followed a tutorial on user profile and when i completed all pages and i create a new account, login using details all work fine till i logout then i cant log in again here is all codeing for this can someone take a look at it x
    heres register.php
    <?php

    include('core/init.inc.php');

    $errors = array();

    if (isset($_POST['username'], $_POST['password'], $_POST['repeat_password'])){
    if (empty($_POST['username'])){
    $errors[] = 'Please fill in your desired username to register.';
    }

    if (empty($_POST['password']) || empty($_POST['repeat_password'])){
    $errors[] = 'Please fill in your desired password to register.';
    }

    if ($_POST['password'] !== $_POST['repeat_password']){
    $errors[] = 'Your passwords didnt match.';
    }

    if (user_exists($_POST['username'])){
    $errors[] = 'The username you entered has already been taken.';
    }

    if (empty($errors)){
    add_user($_POST['username'], $_POST['password']);

    $_SESSION['username'] = htmlentities($_POST['username']);

    header('location: protected.php');
    die();
    }
    }

    ?>
    <!DOCTYPE HTML>
    <html>
    <head>
    <meta http-equiv="content-type" content="text/html" />

    <title>Untitled 11</title>
    </head>
    <body>
    <p>
    <?php

    if (empty($errors) === false){
    ?>
    <ul>
    <?php

    foreach ($errors as $error){
    echo "<li>{$error}</li>";
    }

    ?>
    </ul>
    <?php
    }

    ?>
    </p>
    <form action="" method="post">
    <p>
    <label for="username">Username</label>
    <input type="text" name="username" id="username" value="<?php if (isset($_POST['username']))echo htmlentities($_POST['username']);?>" />
    </p>
    <p>
    <label for="password">Password</label>
    <input type="password" name="password" id="password" />
    </p>
    <p>
    <label for="password">Repeat Password</label>
    <input type="password" name="repeat_password" id="repeat_password" />
    </p>
    <p>
    <input type="submit" value="register" />
    </p>
    </form>

    </body>
    </html>
    heres login.php
    <?php

    include('core/init.inc.php');

    $errors = array();

    if (isset($_POST['username'], $_POST['password'])){
    if (empty($_POST['username'])){
    $errors[] = 'Please enter your registered email address to login.';
    }

    if (empty($_POST['password'])){
    $errors[] = 'Please enter your registered password to login.';
    }

    if (valid_credentials($_POST['username'], sha1($_POST['password'])) === false){
    $errors[] = 'The username/password you entered was incorrect.';
    }

    if (empty($errors)){
    if (isset($_POST['set_cookie']) && $_POST['set_cookie'] == '1'){
    setcookie('username', $_POST['username'], time() + 2,628,000);
    setcookie('password', sha1($_POST['password']), time() + 2,628,000);
    }
    $_SESSION['username'] = htmlentities($_POST['username']);

    header('Location: protected.php');
    die();
    }
    }
    ?>
    <!DOCTYPE HTML>
    <html>
    <head>
    <meta http-equiv="content-type" content="text/html" />


    <title>Untitled 8</title>
    </head>
    <body>
    <div>
    <?php

    if (empty($errors) === false){
    ?>
    <ul>
    <?php

    foreach ($errors as $error){
    echo "<li>($error)</li>";
    }

    ?>
    </ul>
    <?php
    }else{
    echo 'Need an account ? <a href="register.php">Register Here</a>';
    }

    ?>
    </div>
    <form action="" method="post">
    <p>
    <label for="Username">Username</label>
    <input type="text" name="username" id="username" value="<?php if (isset($_POST['username']))echo htmlentities($_POST['username']);?>" />
    </p>
    <p>
    <label for="Password">Password</label>
    <input type="password" name="password" id="password" />
    </p>
    <p>
    <label for="set_cookie">Remember Me</label>
    <input type="checkbox" name="set_cookie" id="set_cookie" value="1" />
    </p>
    <p>
    <input type="submit" value="Login" />
    </p>
    </form>


    </body>
    </html>
    heres logout.php
    <?php

    session_start();

    $_SESSION = array();

    session_destroy();

    if (isset($_COOKIE['username'], $_COOKIE['password'])){
    setcookie('username', '', time());
    setcookie('password', '', time());
    }

    header('Location: login.php');

    ?>

    any help would be much appreciated
  2. #2
  3. --
    Devshed Expert (3500 - 3999 posts)

    Join Date
    Jul 2012
    Posts
    3,923
    Rep Power
    1045
    Oh c'mon.

    Do you really expect anyone to spend time on your problem when you're obviously too lazy to even write a proper post?

    Do some basic debugging yourself and narrow down the problem (var_dump() well help you with that). Give us a concrete error description with all the info you've gathered. Wrap your code in [ PHP ] tags. And then we'll help you.

    Start by checking the return value of valid_credentials(). Go from there.
    The 6 worst sins of security ē How to (properly) access a MySQL database with PHP

    Why canít I use certain words like "drop" as part of my Security Question answers?
    There are certain words used by hackers to try to gain access to systems and manipulate data; therefore, the following words are restricted: "select," "delete," "update," "insert," "drop" and "null".

IMN logo majestic logo threadwatch logo seochat tools logo