0

I think im utilizing php oop right, however its not redirecting the page to the profile.php page it just goes blank after i submit.

No error message shows, i wonder what im dong wrong.

Could it be a session problem ?

index.php

<?php
error_reporting(-1);
require_once 'layouts/header.php';
require_once 'User.php';

session_start();

if (isset($_POST['btn-signup']) ){

    $username = htmlentities($_POST['txt-username']);
    $unpass = htmlentities($_POST['txt-password']);
    $password = password_hash($unpass, PASSWORD_BCRYPT, ['cost' => 12] );
    $unemail = $_POST['txt-email'];
    $email = filter_var($unemail, FILTER_VALIDATE_EMAIL);

    $guest = new User();

    if($username ==""){
        echo "Enter a Username";

    }

    if($email == ""){
        echo "Enter a Email";
    }

    if($password == ""){
        echo "Enter a Password";
    }

    elseif($guest->signup($email,$password,$username)){
        header("Location:profile.php");
    }
}


?>


    <div class="container">
        <div class="row">
                <form action ="" method="POST">
                  <div class="form-group">
                    <label for="Email">Email address</label>
                    <input type="email" class="form-control" aria-describedby="emailHelp" name="txt-email" placeholder="Enter email">
                  </div>

                  <div class="form-group">
                    <label for="Username">Username</label>
                    <input type="text" class="form-control" name="txt-username" aria-describedby="emailHelp" name="txt-username" placeholder="Enter Username">
                  </div>


                  <div class="form-group">
                    <label for="Password">Password</label>
                    <input type="password" class="form-control" name="txt-password" aria-describedby="emailHelp" name="txt-password" placeholder="Enter email">
                  </div>


                     <button type="submit" name="btn-signup" class="btn btn-primary">Submit</button>
                </form>


        </div>
    </div>


</body>
</html>

User.php

<?php

require_once 'Db.php';

class User extends Db{

    private $db;

    public function __construct()
    {
        $this->db = new Db();

    }


    public function signup($email, $password, $username)
    {
        try{
            $stmt = $this->db->connect->prepare("INSERT INTO users (user_email, user_pass, user_name) VALUES (:email, :password, :username) ");

            $stmt->bindparam(':email', $email);
            $stmt->bindparam(':password', $password);
            $stmt->bindparam(':username', $username);
            $stmt->execute();

        }

        catch(PDOExeception $e)
        {
            echo $e->getMessage();
        }


    }


}

Db.php

<?php 

error_reporting(-1);

class Db{

    private $db_host;
    private $db_user;
    private $db_name;
    private $db_pass;

    public function connect()
    {
        $this->db_host = "127.0.0.1";
        $this->db_user = "root";
        $this->db_pass = "root";
        $this->db_name = "eli9";

        try {
            $db = new PDO("mysql:host=127.0.0.1;port=8889,dbname=eli9", 'root', 'root');
            $db->setAttribute( PDO::ATTR_ERRMODE, PDO::ERRMODE_WARNING);
            echo "connected \n";


        } 
        catch (PDOException $e){
            echo $e->getMessage();
        }


        return $db;

    }


}
5
  • This won't fix your code but don't use htmlentities() against passwords (or any other form of manipulation) since it could do more damage than good when using password_verify() and will fail. You also gave the inputs 2x name attributes of the same. Commented May 23, 2017 at 1:59
  • thanks fred, i will keep that in mine. Commented May 23, 2017 at 2:11
  • welcome. Well, TBH I'm not very good with OOP stuff but if you're getting a blank page, it could be caused by a return somewhere. I'd have to copy and test your code but I won't be able to tonight and will give it a try tomorrow, unless I seen a solution posted. Commented May 23, 2017 at 2:53
  • 1
    I also mentioned that two of your inputs have name="xxx" twice; you need to remove one of them from those. That could be having some adverse effects. Commented May 23, 2017 at 3:01
  • i deleted the duplicates name inputs, however now, i get a white screen again... i even changed 'btn-signup to 'btn_signup' Commented May 23, 2017 at 3:04

1 Answer 1

0

session_start() must be placed at the very beginning of a script before it produce any visible output. But in your case, it seems that

require_once 'layouts/header.php';

might have some html or other output. Place the session_start() code on top of that could solve your problem.

<?php
error_reporting(-1);
session_start();
require_once 'layouts/header.php';
require_once 'User.php';

if (isset($_POST['btn-signup']) ){

    $username = htmlentities($_POST['txt-username']);
    $unpass = htmlentities($_POST['txt-password']);
    $password = password_hash($unpass, PASSWORD_BCRYPT, ['cost' => 12] );
    $unemail = $_POST['txt-email'];
    $email = filter_var($unemail, FILTER_VALIDATE_EMAIL);

    $guest = new User();

    if($username ==""){
        echo "Enter a Username";

    }

    if($email == ""){
        echo "Enter a Email";
    }

    if($password == ""){
        echo "Enter a Password";
    }

    elseif($guest->signup($email,$password,$username)){
        header("Location:profile.php");
    }
}


?>
Sign up to request clarification or add additional context in comments.

8 Comments

still didnt work i did what you said. i put the session_start() at the top and nothing happened still
Oh, I see. There is also a big problem in your button and field name. You can not put dash (-) on name. please replace them with underscore in your form and script.
"You can not put dash (-) on name. please replace them with underscore in your form and script." - @Chayan That is false. Name attributes can contain hyphens, just not variables, since PHP would interpret $var-x as var minus x, not name attributes. You shouldn't pass off false information like that (and to the OP) and it will not fix their code.
@Chayan thanks it works nowww :))) thank you chayan and fred
@OwlMan are you saying that changing the hyphens to underscores worked? if not, what did?
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.