Jump to content

require_once message shows up in local server but does not show up in live server


bee65

Recommended Posts

I'm trying to make a simple website where people register to my website.

When the user doesn't fill anything inside the boxes they get a message "Please fill all required fields" on the register.php page

On my local host require_once works good. It shows up.

 

But when i upload the files to my sever the require_once does not show up on the register.php

It just refreshes and i dont get the message "Please fill all required fields"

 

This is the code that works in local host but not in a live server

<?php require_once 'messages.php'; ?>

 

 

Here is my full code

 

Register page:

<html>


<?php require_once 'messages.php'; ?>

<br><br>


<form action="register-clicked.php" method="POST">

Username:<br>
<input type="text" name="usernamebox" placeholder="Enter Username Here">
<br><br>
Email:<br>
<input type="text" name="emailbox" placeholder="Enter email here">
<br><br>
Password:<br>
<input type="password" name="passwordbox" placeholder="Enter password here">
<br><br>
Confirm Password:<br>
<input type="password" name="passwordconfirmbox" placeholder="Re-enter password here">
<br><br>

<input type="submit" name="submitbox" value="Press to submit">
<br><br>

</form>

</html>

 

 

Register clicked

<?php


session_start();



$data = $_POST;

if( empty($data['usernamebox']) ||
    empty($data['emailbox']) ||
    empty($data['passwordbox']) ||
    empty($data['passwordconfirmbox'])) {
    $_SESSION['messages'][] = 'Please fill all required fields';
    header('Location: register.php');
    exit;

}




if ($data['passwordbox'] !== $data['passwordconfirmbox']) {
    $_SESSION['messages'][] = 'Passwords do not match';
    header('Location: register.php');
    exit;
}


$dsn = 'mysql:dbname=mydatabase;host=localhost';
$dbUser='myuser';
$dbPassword= 'password';

try{
$connection = new PDO($dsn, $dbUser, $dbPassword);
} catch (PDOException $exception){
    $_SESSION['messages'][] = 'Connection failed: ' . $exception->getMessage();
    header('Location: register.php');
    exit;
}

 

 

messages.php

<?php
session_start();

if (empty($_SESSION['messages'])){
    return;
}

$messages = $_SESSION['messages'];
unset($_SESSION['messages']);
?>



<ul>
    <?php foreach ($messages as $message): ?>
        <li><?php echo $message; ?></li>
    <?php endforeach; ?>
</ul>

 

Edited by bee65
Link to comment
Share on other sites

the session_start() in the messages.php file is probably failing, due to the html markup you are outputting on the register.php page, before the point where you are requiring messages.php. if you set php's error_reporting to E_ALL and display_errors to ON, you should find the actual reason for the code not working. the reason this works on your development system and not on the live server is most likely due to the output_buffering setting, in the php.ini, being set to on (a non-zero integer value) on your development system. you can check both systems using a phpinfo() statement in a .php script file to confirm this. since you may not be able to change this setting on the live server, it will be best if you turn this setting off on your development system, so that any code you produce won't stop, due to this problem, when you move it to a live server.

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 for the dynamic content on the page
  4. html document

your page should have one session_start() statement, it should either be directly or indirectly (via a required file) in the initialization section.

item #2 means that your form processing code should be on the same page as the form. this will simplify all the code and provide a better User eXperience (UX), since you can re-populate the relevant form field values when you display any validation/user error messages and re-display the form.

btw - you should NOT let the user know if a database or any other internal error has occurred. this information is only useful to you, the programmer/developer. you should display errors like this when you are learning, developing, and debugging code/query(ies) and log this information when on a live server.

here's some additional points for the database connection code -

  1. name the connection variable $pdo or similar so that anyone looking at the code will know what it contains.
  2. set the character set to match your database table(s.)
  3. set the error mode to exceptions. the connection always uses an exception for errors, you want to the other database statements - query, prepare, and execute, to do the same, so that you don't need to write out a bunch of program logic to handle errors.
  4. set emulated prepared queries to false. you want to run real prepared queries.
  5. set the default fetch mode to assoc, so that you don't need to keep specifying it in every fetch statement.
Edited by mac_gyver
Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.