1

In this chunk of code it was previously designed to use the session_id. I am trying to convert from using the session_id to using a User ID that is retrieved from the database. I'm not sure what I did wrong but the function is not returning the variable. Any suggestions would be appreciated.

 protected function get_user_id() {
        //previous code used the session id
        //@session_start();
        //return session_id();

        // New code to use User ID instead of session_id
        // Connecting to the database
        include ("../../../admin/includes/connect.php");

        // Let's get the user ID from the database for use with the widget
        $user_id_query = "SELECT nonadmin_user_id FROM `nonadmin_user_login` WHERE email = '$_SESSION[email]'";
        $run_query = mysqli_query($conn, $user_id_query);
        while($row=mysqli_fetch_array($run_query)){

        // Create variable for the user's id
        $nonadmin_user_id = $row['nonadmin_user_id']; }
        return $nonadmin_user_id;
    }
    // This function needs to use the variable $nonadmin_user_id
    protected function get_user_path() {
        if ($this->options['user_dirs']) {
            return $this->get_user_id().'/';
        }
        return '';
    }
6
  • 1
    Could be a scope issue, plus did you start the session? Add error reporting to the top of your file(s) right after your opening PHP tag for example <?php error_reporting(E_ALL); ini_set('display_errors', 1); then the rest of your code, to see if it yields anything, as well as or die(mysqli_error($conn)) to mysqli_query(). If it works without using the custom functions, then it's a scope issue. Commented Mar 28, 2015 at 2:49
  • Fred thank you for your response. This file is included in a file that is included in the file that has the session start so as far as I know the session is started. Not sure that matters since I am not trying to use the session ID anyway. I added the code snippet you gave for error reporting and no errors are reported. As you mentioned the code works until I modified it. I'm just not sure why the modification has caused the breakage. Commented Mar 28, 2015 at 2:59
  • You're welcome. If you worked from a copy of it, check to see what you changed and compare it with the code that isn't working. Use var_dump(); and see what's passing through or not. Plus, echo your query also; see what's going in there or not. Commented Mar 28, 2015 at 3:02
  • since I am not trying to use the session ID anyway - So, why are you doing WHERE email = '$_SESSION[email]'? Commented Mar 28, 2015 at 3:10
  • Fred you're the man! It was the session. I removed the comment out from in front of the session start and now it works perfect. What baffles me on this is I was under the impression that if you start a session in a file and then include other files the included files did not require the session to be started. Thank you for your help sir! Can you fix your comment into an answer so I can mark this as answered? Commented Mar 28, 2015 at 3:12

1 Answer 1

1

"Fred you're the man! It was the session. I removed the comment out from in front of the session start and now it works perfect. What baffles me on this is I was under the impression that if you start a session in a file and then include other files the included files did not require the session to be started."

The session needs to be started in order for the session array to be recognized and passed successfully in your query.

Plus, session_start(); is required to be resident inside all files using sessions.


Add error reporting to the top of your file(s) which will help find errors.

<?php 
error_reporting(E_ALL);
ini_set('display_errors', 1);

// rest of your code

Sidenote: Error reporting should only be done in staging, and never production.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.