I am typically new to using an online web server. 000webhost.com is a free web hosting server site that provides a database and you can also connect your website with it. I have created my database fine and when I have tried to connect the database and the website. I think the problem is within connecting to the database and I don't have any idea what do I input. this error prompts
this is how i set up my connection.
<?php
// define database related variables
$database = '..';
$host = '..';
$user = '..';
$pass = '..';
// try to conncet to database
$dbh = new PDO("mysql:dbname={$database};host={$host};port={3306}", $user, $pass);
if(!$dbh){
echo "unable to connect to database";
}
?>
and this is my authenticate.php
<?php
require 'database-config.php';
session_start();
$username = "";
$password = "";
if(isset($_POST['username'])){
$username = $_POST['username'];
}
if (isset($_POST['password'])) {
$password = $_POST['password'];
}
echo $username ." : ".$password;
$q = 'SELECT * FROM users WHERE username=:username AND password=:password';
$query = $dbh->prepare($q);
$query->execute(array(':username' => $username, ':password' => $password));
if($query->rowCount() == 0){
header('Location: index.php?err=1');
}else{
$row = $query->fetch(PDO::FETCH_ASSOC);
session_regenerate_id();
$_SESSION['sess_user_id'] = $row['id'];
$_SESSION['sess_username'] = $row['username'];
$_SESSION['sess_userrole'] = $row['role'];
echo $_SESSION['sess_userrole'];
session_write_close();
if( $_SESSION['sess_userrole'] == "admin"){
header('Location: adminhome.php');
}else{
header('Location: userhome.php');
}
}
?>
because these are what the site has given me
I hope i can solve this right away because I will be planning to connect it thru mobile soon. Thanks in advance sirs.
Your problem is that you send data before sending a header. In your case the username:password is causing the errors. You have to echo them after all the header
s and session_*
. Just call the echo at the end of this script and you should be fine. And please never post any credentials (especially no DB credentials)
<?php
require 'database-config.php';
session_start();
$username = "";
$password = "";
if(isset($_POST['username'])){
$username = $_POST['username'];
}
if (isset($_POST['password'])) {
$password = $_POST['password'];
}
echo $username ." : ".$password; //<-- Causing the problem
$q = 'SELECT * FROM users WHERE username=:username AND password=:password';
$query = $dbh->prepare($q);
$query->execute(array(':username' => $username, ':password' => $password));
if($query->rowCount() == 0){
header('Location: index.php?err=1');
}else{
$row = $query->fetch(PDO::FETCH_ASSOC);
session_regenerate_id();
$_SESSION['sess_user_id'] = $row['id'];
$_SESSION['sess_username'] = $row['username'];
$_SESSION['sess_userrole'] = $row['role'];
echo $_SESSION['sess_userrole']; //<-- Causing the second problem
session_write_close();
if( $_SESSION['sess_userrole'] == "admin"){
header('Location: adminhome.php');
}else{
header('Location: userhome.php');
}
}
?>