My website isn't working on host server

Hello,

My website functioned with no problems on WAMP server at home.

I uploaded it to a hosting server I selected. I changed the connection settings accordingly

Here is the connection code:

try {
  $conn = new PDO("mysql:host=$servername;dbname=$db_name", $username, $password);
  // set the PDO error mode to exception
  $conn->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
  
  //echo "Connected successfully";
} catch(PDOException $e) {
  echo "Connection failed: " . $e->getMessage();
}

I got a mesage Connected successfully and I turned it to a comment

When I click the login button I get the page in screenshot


I searched for explanations q solutitions. Everything I found was about Wordpress websites which is what my website is not.
What do I do now ?

P.S. Please ignore the design.

A 500 could be a number of things.
What does your error log say?

I followed instructions on this site :slight_smile: https://www.makeuseof.com/find-error-logs-to-troubleshoot-chrome/

my log.txt (19.6 KB)

Here is the log file content.
I don’t know what to look for

That’s a browser (client side) log. You need to look at the server side logs. The PHP (since PHP appears to be executing to some extent) or the Apache server logs.

Here is the server log file:
SERVER_LOG.TXT.txt (6.7 KB)

Here is the beginning of header.php and page_header.php

<?php
if(!isset($_SESSION)){
  session_start();
  }

line 3 is session_start();

I don’t understand,
When I attended webmaster classes I was told that every php file has to begin with session start where there is a login process ansd a session.

Than I got an error when a session existed so the solution was adding a condition
if(!isset($_SESSION)){

It worked on WAMP server

Now on a real host It generates an error.

what does it mean

Session cannot be started after headers have already been sent
mean ? why doesn’t the condition solve the problem ?

How Do I solve this problem now ?

1 Like

Unfortunately, php released a couple of php7.x versions where this error message didn’t state the actual cause of the problem.

So, two problems - 1) you are using an old and out of date php version and should update to at least php8, and 2) you probably have some output occurring before the session_start() statement.

Either you have some output in the main file, before the include statement statement for this file, or one or more of your files has been saved with BOM (Byte Order Mark) characters and should be edited and saved without the BOM characters.

1 Like

Session_start is only warning. It doesn’t give out 500 error. You need to check:

  1. Using correct mySQL connection yet: server name, username, password.
  2. The path. I see with some plugins, they have initial path in their config files. Then if you move to another host, with different path, the code is no longer working. You will need to check that too.
1 Like

I experienced a similar problem and don’t really understand headers to be honest. But running a PHP page script can result in the page sending information via headers. I solved this by ensuring that session_start(); was as close as possible to the top of the script. Any HTML or even a blank line can result in header info being sent. With no disrespect intended, it sounds like you are by no means an expert, and neither am I. I would start by simplifying your code and getting a connection working then begin adding code back in. For example forget the try and catch and just connect for now. Also do you really need

if(!isset($_SESSION)){
  session_start();
  }

at this stage, if it is at the beginning of a new script. If it’s a new script and not an include, surely you need to start the session anyway, so just session_start(); should suffice.

1 Like

Any HTML or even a blank line (or even a single space or newline character) will definitely result in header info being sent, unless you enable output buffering. There needs to be nothing at all before the opening PHP tag.

3 Likes

I would start by creating a simple HTML file (such as index.html) to ensure that much works. Actually, even before that, I would check to ensure that the DNS has the correct NS and A records.

I would have thought that the fact that he can connect and his own custom ‘Connected Successfully’ echo displays OK would rule out most of those problems. The unhelpful error message 'This page isn’t working is often an indication that there is a PHP error - often syntax - but the server is configured not to display detailed error messges.
Also -

would indicate that the server is working and the error is in his script

1 Like

The ‘unhelpful’ error message is a default configuration because most sites in the world are production sites - where you dont want to display meaningful errors to end users, who might use them maliciously. :wink: The specific error message is output into the server’s error log file. Which… yaknow… should be somewhere secure that end users cant see.

2 Likes

Hello,
I made a progress yet not all problems solved

  1. Where am I supposed to put session_start(); ?
    In my website there are pager which are visible to end users and ther are pages not visible like login.inc.php. Here is its code:
 <?php
//session_start();

if (isset($_POST['submit'])) {
    include_once 'db_connect.inc.php';

    $username = $_POST['username'];
    $password = $_POST['password'];

    if (empty($username) || empty($password)) {

        $_SESSION['login_error'] = "Login Error: Missing username and/or password";
		header("Location: ../index.php");
        exit();
    } else {
		
        $sql = "SELECT * FROM `users` WHERE username = :username LIMIT 1";
		
		$stmt = $db->prepare($sql);
		
        $stmt->bindParam(':username', $username, PDO::PARAM_STR);
		
		
        $stmt->execute();
        $row = $stmt->fetch(PDO::FETCH_ASSOC);

        if (!$row) {
            $_SESSION['login_error'] = "Login Error: Incorrect username or password";
            header("Location: ../index.php");
            exit();
        } else {
            $hashedPasswordCheck = password_verify($password, $row['password']);
            if ($hashedPasswordCheck == false) {
                $_SESSION['login_error'] = "Login Error: Incorrect username or password";
                header("Location: ../index.php");
                exit();
            } elseif ($hashedPasswordCheck == true) {
                $_SESSION['user_id'] = $row['user_id'];
                $_SESSION['firstname'] = $row['firstname'];
                $_SESSION['lastname'] = $row['lastname'];
                $_SESSION['username'] = $row['username'];
                $_SESSION['LAST_REQUEST_TIME'] = date("Y-m-d H:i:s");
                $_SESSION['login_error'] = "";
				
                header("Location: log.php");
                exit();
            }
        }
    }
}
?>

the line with session_statr(); generated an error in error_log file which no longer shows since it is a comment now - and its presence there seeems to be a mistake.
so which pages should start with session_start(); ? and which pages shouldn’t ? (I have been taught or at least i understood that way that every page shoult start with session_start(); at the top)
2. Here are 2 lines from the server eror_log file:

[26-Aug-2023 07:18:31 America/Chicago] PHP Warning: Cannot modify header information - headers already sent by (output started at /home4/traderan/public_html/includes/login.inc.php:1) in /home4/traderan/public_html/includes/login.inc.php on line 44

[26-Aug-2023 07:40:32 America/Chicago] PHP Warning: Cannot modify header information - headers already sent by (output started at /home4/traderan/public_html/includes/login.inc.php:1) in /home4/traderan/public_html/includes/login.inc.php on line 13

This is line 13
header("Location: ../index.php");
And this is line 44:
header("Location: log.php");

I want that if there is a login problem to direct the user to index.php with a message, while If login process is successful to continue to next step which registers a successful user login in log.php
This logic generate an error. how can I solve this problem ?

1 Like

Any page that uses (sets or references a session variable) needs a session start statement.

In general, the code for any page should be laid out in this order -

  1. initialization
  2. post method form processing
  3. get method business logic - get/produce data needed to display the page
  4. html document

The session start goes in the initialization section, prior to any code that uses a session variable. If you organize your code like this, there won’t be any intentional output from your code, because all intentional output will be in the html document section. You will only have to deal with any unintentional output. Also, if you organize your code like this, you will only ever have one session start statement per page, and you won’t ever need any ridiculous conditional logic to decide if you should start a session.

Your post method form processing and post method form should be on the same page. This results in the least amount of code, that’s easier to secure, and provides the best user experience.

What was that error message? You need to find and fix what’s causing problems. Just making errors go away, in this case by commenting out the line of code, doesn’t fix anything.

For this particular error and the posted code, it appears that you do have a space or some other character before the opening <?php tag. Your task is to find what output is occurring on line 1 of login.ini.php and eliminate it.

1 Like

Check if your domain is correctly pointed to the server . If it is and DNS propagation has passed a long time ago, global issues on the server your domain is hosted on might be taking place. These issues may be caused by scheduled or unscheduled maintenance, software update, or temporary technical issues.