0

I am still new to programming and learning OOP. I have recently discovered how to make classes in PHP and how to access them. Before I describe the issues, I can easily make workarounds but I am making every stride to implement/learn to code in a professional manner and using DRY code.

The issue I am encountering is trying to access a single Object which I aim to use in other php files, while the user is logged in. For instance, the user will enter their credentials to log in, this will initialize a "User" object used to verify the password. Once verified the user is redirected to another php file/page. However, at this point, I am finding that the Object is destroyed and I do not have access to the initial one created in that first step. (it returns null)

I know I am still learning, but creating more than one object for the same user sounds like a waste of resources to me and does not follow DRY code.

I created a php file (test.php) which is used to enter the login credentials, that check a local PHP DB. If the credentials are found the user is redirected to another page (test2.php).

On the redirect page (test2.php) I have included access to the same file and also attempt to call a method on the initialized object.

<?php

    include 'classes/connection.class.php';
    include 'includes/sql.inc.php';
    include 'classes/userfuncs.class.php';

    $user;

if(isset($_POST['submit'])) {
    $email = $_POST['email'];
    $pwd = $_POST['pwd'];

    $user = new User();
    $loginStats = $user -> verifyAcct($email, $pwd);

    if($loginStats['acctVerified']) {
        header("location: test2.php");
    } else {
        header("location: test.php");
    }
    
}
<?php
    //test2.php
    require "userobj.inc.php";
    $user->page1Msg();
?>

After entering proper credentials at the login page (test.php), I am successfully being redirected to the 2nd page (test2.php) however I am given an "Undefined variable" message which lets me know that I am unable to access that initial object. I expect a simple echo message to be displayed from the method called on that 2nd page, however, that message will only display if the Object still exists or is recreated. Again, I am unsure if this is proper code practice, but I do not wish to recreate the object.

The message being given is:

Notice: Undefined variable: user in /Applications/XAMPP/xamppfiles/htdocs/tutorials/php/IEX-Update/test2.php on line 3

Fatal error: Uncaught Error: Call to a member function page1Msg() on null in /Applications/XAMPP/xamppfiles/htdocs/tutorials/php/IEX-Update/test2.php:3 Stack trace: #0 {main} thrown in /Applications/XAMPP/xamppfiles/htdocs/tutorials/php/IEX-Update/test2.php on line 3

Community
  • 1
  • 1
  • ___however I am given an "Undefined variable" message___ Dont keep it a secret, ALWAYS show us the message... ALL the message, not a summary of the message – RiggsFolly Jul 11 '19 at 16:15
  • Not a problem, I have modified the question. Thanks – Eddie Melendez Jul 11 '19 at 16:22
  • Remember! If its a different script then it knows NOTHING that was done in any other script.. So require the class code again and instanitate the class again – RiggsFolly Jul 11 '19 at 16:25
  • I figured that much, but is this the proper way to do it? I am essentially creating the same Object twice, which I believe is a waste of resources, loading, speed, etc. one idea I thought is to create a specific file/object to verify login creds, but I am still stuck on how I would retrieve the users login information on the redirect page to create a User object. – Eddie Melendez Jul 11 '19 at 16:30
  • Do some reasearch on the SESSION I think thats the direction you may want to go in – RiggsFolly Jul 11 '19 at 16:31
  • And remember its not 2 objects at the same time The first script finishes and all its data is destroyed. Then the second script starts – RiggsFolly Jul 11 '19 at 16:33
  • You cant, not in the way you are attempting to – RiggsFolly Jul 11 '19 at 16:37
  • Is there a way to do it? Is this even necessary? How does a company do it? (if they were to use PHP) I am trying to learn how to properly go about this. I am not set in my ways to execute this in this way. If that means trashing and starting from scratch that is fine. I am simply trying to figure out the industry standards on how this is handled so I can execute it properly. – Eddie Melendez Jul 11 '19 at 16:45
  • Hmmm, perhaps you're right about "Sessions" maybe there's another way to go about it. Got my gears moving. Thank you. – Eddie Melendez Jul 11 '19 at 16:52
  • You could dehydrate the properties in an object into a Session var and then rehydrate the properties back into the newly Instantiated onject in a second script – RiggsFolly Jul 11 '19 at 16:54
  • ALthough I have NEVER found a need to do that in anything I have ever written – RiggsFolly Jul 11 '19 at 16:55
  • I made it work. I couldn't completely follow DRY code. I currently make 3 objects in the entire project. One to login, one to logout, and one continuous in the pages/header of the actual site. & I've maintained some helpful (non-personal) data in the session. – Eddie Melendez Jul 14 '19 at 01:27

0 Answers0