I created this custom website which has a login page. There's no wordpress or any CMS involved. It's working fine when I run the website in the local server. I'm not really sure how to host it in a shared server despite of trying many times, and migrate the simple database I have which has 4 columns(index, username, email, password).
Errors recieved:
Warning: mysql_connect(): Access denied for user 'root'@'localhost' (using password: NO) in /home4/ablev3/public_html/connect.php on line 2
Database Connection FailedAccess denied for user 'root'@'localhost' (using password: NO)
Here's the code of my connect.php file:-
<?php
$connection = mysql_connect('localhost', 'root', '');
if (!$connection){
die("Database Connection Failed" . mysql_error());
}
$select_db = mysql_select_db('ablev3_test');
if (!$select_db){
die("Database Selection Failed" . mysql_error());
}
What should I keep instead of 'localhost'and 'root' when I upload the file to hostgator shared server? Seems like that were im getting the error. Thanks.
Will appreciate any help.
##Attempt 2##
I tried the suggestions in the following order, still unresolved, what am I doing wrong? Please see below:
- I made a new database
- Created and added a new user and set new password for that database
- Linked the user to the database
- Changed 'root' to replace with my DB username
- Changed '' to my DB password
Now errors I get(when typing the domain name) looks like this:
Warning: session_start(): Cannot send session cache limiter - headers already sent (output started at /home4/ablev3/public_html/index.php:1) in /home4/ablev3/public_html/index.php on line 2
My connect.php code, after changes:-
<?php
$connection = mysql_connect('localhost', 'MyDatabaseUsername', 'MyDatabasePassword');
if (!$connection){
die("Database Connection Failed" . mysql_error());
}
$select_db = mysql_select_db('NameOfMyNewDatabase');
if (!$select_db){
die("Database Selection Failed" . mysql_error());
}
My index.php code after changes:-
<?php
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 `tableOTA` WHERE username='$username' and password='$password'";
$result = mysql_query($query) or die(mysql_error());
$count = mysql_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.
echo "Invalid Login Credentials.";
}
}
//3.1.4 if the user is logged in Greets the user with message
if (isset($_SESSION['username'])){
header( 'Location:introduction.php' );
$username = $_SESSION['username'];
echo "Hai " . $username . "";
echo "This is the Members Area";
echo "<a href='logout.php'>Logout</a>";
}else{
//3.2 When the user visits the page first time, simple login form will be displayed.
?>
<!DOCTYPE html>
<html>
<head>
<link rel="stylesheet" type="text/css" href="style.css" />
</head>
<body>
<!-- Form for logging in the users -->
> <div class="register-form">
..............
..........
.........
What am I doing wrong? how to rectify this error?
