0

I have a very basic log in system, which I have tested on my localhost as well as a free hosting site I use to test my projects and all worked fine.

I have just moved the site to Site Ground as a subdomain and the login has stopped working. The site is still loading content from the database I have created in this location, so I know the issue is not a result from a failure to connect to the database.

There is nothing in the .htaccess file to block a user login either, if it is an issue with moving it to a new server, how would I find a way around this? The page does NOT return any error when attempting to login

login PHP:

<?php

require 'includes/connect.php';

session_start();

if(isset($_POST['login'])){
$username = $_POST['username'];
$password = md5 ($_POST['password']);


$pdo = Database::connect();
$pdo->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
$sql = "SELECT COUNT(user_id) FROM nathan WHERE username='$username' AND password='$password'";
$q = $pdo->prepare($sql);
$q->execute();

$count = $q->fetchColumn();

if($count == 1){
    $_SESSION['username'] = $username;

    header('Location: admin.php');
} 
}        

?>

DB connection:

<?php
class Database
{
private static $dbName = "some_DB" ;
private static $dbHost = "localhost" ;
private static $username = "some_user";
private static $password = "password123";

private static $cont  = null;

public function __construct() {
    die('Init function is not allowed');
}

public static function connect()
{
   // One connection through whole application
   if ( null == self::$cont )
   {     
    try
    {
      self::$cont =  new PDO( "mysql:host=".self::$dbHost.";"."dbname=".self::$dbName, self::$username, self::$password); 
    }
    catch(PDOException $e)
    {
      die($e->getMessage()); 
    }
   }
   return self::$cont;
}

public static function disconnect()
{
    self::$cont = null;
}
}
?>

Session PHP:

<?php

session_start();

if (!isset($_SESSION['username'])){
header('Location: login.php');
};
?>
eschift
  • 39
  • 3

2 Answers2

1

You might want to inspect your cookies when viewing and verify that you indeed are getting a cookie set with session information. If this isn't working, my limited view of your environment would suggest that you're not allowing cookies from that specific subdomain.

Another thing to check is the directory where sessions data is stored. If that isn't populated, then you're not going to be able to persist a session.

sebi
  • 79
  • 6
0

As the poster above me said it is definitely because of the cookies, refer to this post for solutions PHP Sessions across sub domains

And you shouldn't store password directly in the database, it is insecure, you should hash it with password_hash(), and then compare it with hash_equals(), on login.

Community
  • 1
  • 1
Sasa Blagojevic
  • 2,110
  • 17
  • 22