Jump to content

Recommended Posts

  • Solution

I have the following code for a very simple registration form handler -

 

  //require('../../config/config.php');
  require('reg_functions.php');

  //assign clean form variables
  $firstname = clean_names($_POST['firstname']);
  $lastname = clean_names($_POST['lastname']);
  $username = clean_names($_POST['username']);
  $email = clean_email($_POST['email']);
  $email2 = clean_email($_POST['email2']);
  $password = clean_password($_POST['password']);
  $password2 = clean_password($_POST['password2']);
  $date = date("Y-m-d");


  if(isset($_POST['register_button'])) {

    //validate & confirm emails match
    if($email == $email2) {

      if(filter_var($email,  FILTER_VALIDATE_EMAIL)) {
        $email = filter_var($email,  FILTER_VALIDATE_EMAIL);
        
        // I THINK I COULD DEF MAKE THIS EMAIL CHECK CLEANER - WORK ON THAT LATER
        //check if email exists in system already
        $email_check = mysqli_query($conn, "SELECT email FROM users WHERE email='$email'");

        //number of rows returned with the email (this should be 0 to proceed)
        $number_rows = mysqli_num_rows($email_check);

        if($number_rows > 0) {
          echo "Email already in use <br>";
        } else {
          echo "email is correct <br>";
        }

      } else {

        echo "Invalid email format <br>";

      }

    } else {

      echo "Emails don't match <br>";

    }

    echo "First Name: " . $firstname . "<br>";
    echo "Last Name: " . $lastname . "<br>";
    echo "Username: " . $username . "<br>";
    echo "Number of Rows: " . $number_rows . "<br>";

  }

the code that starts with the mysqli_query() function to check for matching emails is where it fails (white screen). I figured it may be because the $conn variable wasn't being associated with the $conn variable in the config.php file. So I added the require config.php at the top.

Now it only works if both are commented out.  So in reality this could be 2 issues and I struggled with how not to ask too many questions in one post though they may be the same issue. I'm really new to php.

Can someone explain the mechanics of whats going on (or not going on). to recap, this is the code that caused the white screen initially -

// I THINK I COULD DEF MAKE THIS EMAIL CHECK CLEANER - WORK ON THAT LATER
        //check if email exists in system already
        $email_check = mysqli_query($conn, "SELECT email FROM users WHERE email='$email'");

        //number of rows returned with the email (this should be 0 to proceed)
        $number_rows = mysqli_num_rows($email_check);

        if($number_rows > 0) {
          echo "Email already in use <br>";
        } else {
          echo "email is correct <br>";
        }

and I realized it had no association to the $conn variable in the config.php file so I added the require config.php line at the top. This above code is obviously still not working and if I comment it out, the require config.php line still causes a white screen. This is my config file -

ob_start();
session_start();

$timezone = date_default_timezone_set("America/xxxxxxxx");

$db_host = "localhost";
$db_user = "root";
$db_pass = "";
$db_name = "qcic";

$conn = mysqli_connect( $db_host, $db_user, $db_pass, $db_name );

if( $conn->connect_errno) {

    printf( "Connect Failed: %s\n", $conn->connect_error);
    exit();
    
} else {
    echo "Connected to database";
}

ob_end_flush();

and I get the "Connected to database" echo so I have no idea what the problem is.

Anyone with some time and more experience that can explain some of these things? thanks

Link to comment
https://forums.phpfreaks.com/topic/312871-configphp/
Share on other sites

This was solved by simply replacing require() with include() which brings more questions.

I was under the impression that non critical files should use include and require should be used for critical matters.

In my above reg form the config.php file is definitely critical but I get a blank screen after submitting the data if I use require here and everything is fine using include. Why?

Link to comment
https://forums.phpfreaks.com/topic/312871-configphp/#findComment-1587058
Share on other sites

Because PHP could not open the file.

If you didn't see any error about that fact then it means you do not have your PHP environment set up properly for development. Find your php.ini, go to the bottom of the file, and add

error_reporting = -1
display_errors = 1

then restart your webserver and/or PHP. Then try with the code using require() as it was before and make sure you see the error message(s) from PHP.

 

6 minutes ago, TechnoDiver said:

I was under the impression that non critical files should use include and require should be used for critical matters.

That's one way to think about it, however there are virtually no reasons why you should ever be trying to load a PHP file that isn't necessary for your code to continue running.
And that's the difference between them: both will look for the file and run it if it exists, but include() will let you continue running if it did not. Which raises the question of why you were trying to include() a file that did not exist.

Rule of thumb: always use require().

Link to comment
https://forums.phpfreaks.com/topic/312871-configphp/#findComment-1587059
Share on other sites

Thanks.

I added a line in the config file to echo 'connected to db' and I was seeing that line echoed so thought I was connecting fine. All the error messages told me otherwise.

I'm still experiencing some issues with the config.php file though.

I'm using XAMPP and my relevant dir structure is:

-htdocs

 |-/site

   | - /assets

      |- files you'd expect to find here

   | - /loginphp

      | - register.php

      | - /handlers

         | - register_handler.php

   | - config

      | - config.php

 

It looks to me like the correct relative path from register_handler.php is ../../config/config.php (up 2 directories and into config/config.php but I get the following error -

Warning: require_once(/qcic/config/config.php): Failed to open stream: No such file or directory in /opt/lampp/htdocs/site/loginphp/register.php on line 2

Fatal error: Uncaught Error: Failed opening required '/qcic/config/config.php' (include_path='.:/opt/lampp/lib/php') in /opt/lampp/htdocs/site/loginphp/register.php:2 Stack trace: #0 {main} thrown in /opt/lampp/htdocs/site/loginphp/register.php on line 2

 

I've tried every relative path from '../../config/.....', '../config/........', etc

I couldn't even get the absolute path (/site/config/config.php) to work.  Watched a few videos on config.php files, couldn't find anything useful.

I feel like this is one of those really simple things that my inexperience is stopping me from noticing. Could you shine any light on what I'm doing incorrectly or need to be doing MORE correctly?

 

 

 

 

 

Link to comment
https://forums.phpfreaks.com/topic/312871-configphp/#findComment-1587067
Share on other sites

The path is relative to your document root and cannot go outside. If your absolute path is outside of document root then you need to check permissions. That path must give read permission to whatever user is running httpd. Which is likely different than the user doing the development which may have r/w permissions.

Edited by gw1500se
Link to comment
https://forums.phpfreaks.com/topic/312871-configphp/#findComment-1587068
Share on other sites

It wouldn't let me edit my last comment but I realized this absolute path won't work when the site goes live. I'll have to go through and change the paths to config.php when that time comes. It's not outside of doc root. What path format should be used in this situation so the paths won't need to be changed later?

Link to comment
https://forums.phpfreaks.com/topic/312871-configphp/#findComment-1587071
Share on other sites

5 minutes ago, TechnoDiver said:

It's not outside of doc root

You can and should put configuration files outside of the document root folder.

The variable $_SERVER['DOCUMENT_ROOT'] contains the path to the current document root folder, which you can then concatenate the desired path to the configuration file to, to arrive at the actual path.

Link to comment
https://forums.phpfreaks.com/topic/312871-configphp/#findComment-1587072
Share on other sites

Ok, I get that. The doc root is /opt/lampp/htdocs.

The path to config.php is /opt/lampp/htdocs/site/config/config.php

So do you mean that it's best to move /config/config.php from the /site/ directory to the /htdocs/ directory? Or even outside /htdocs/ to somewhere else?

Link to comment
https://forums.phpfreaks.com/topic/312871-configphp/#findComment-1587073
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.