Page 1 of 1

MVC won't show sessions

Posted: Thu Oct 02, 2014 9:38 am
by jacques1
Hello. I'm trying to create an MVC application, but it seems like my sessions won't show up. Session already starts and I know because I tested it using a customized variable I did. I know that this isn't how MVC is suppose to run, but I'm trying to make my own application that sort of runs MVC. I don't get why my sessions won't show up, but it is set.

Here is my code.

index.php

Code: Select all

<?php
include(dirname(__FILE__) . '/controller/config.php');

$new = new New();
$new->run();
config.php

Code: Select all

<?php
define("MYSQLI_HOST", "localhost");
define("MYSQLI_USERNAME", "username");
define("MYSQLI_PASSWORD", "password");
define("MYSQLI_DATABASE", "database");
define("GUEST_EMAIL", "guest@localhost");
define("WEBSITE_TITLE", "");

if(version_compare(PHP_VERSION, "5.2.0", ">")) {

	class New {

		function run() {

			include('user.php');

			$User = new User();
			$User->view();

		}

	}


} else {
	echo "Sorry, but your PHP Version is less than 5.2.0. Please update your PHP Version so that you are able to use this application.";
}
user.php

Code: Select all

<?php
class User extends New {

	function view() {

		session_start();

		$_SESSION['email'] = "test@test.com";

		// Since email IS set, then the email will output test@test.com, but when you use session_destroy(); here
		// you'll get guest@localhost
		echo $_SESSION['email'] . "<br />";

		if(isset($_SESSION['email'])) {

			if(GUEST_EMAIL == $_SESSION['email']) {
				echo GUEST_EMAIL;
			} else {

				$GET_USERS_DATABASE = new mysqli(MYSQLI_HOST, MYSQLI_USERNAME, MYSQLI_PASSWORD, MYSQLI_DATABASE);
				if($GET_USERS_DATABASE->connect_errno) {
					echo "Error, failed to connect to MySQL database. Please fix.";
					exit();
				}

				$GET_USERS = $GET_USERS_DATABASE->prepare("SELECT id, firstname, lastname, email FROM users WHERE email = ? LIMIT 1");
				$GET_USERS->bind_param("s", $SESSION_EMAIL);
				$SESSION_EMAIL = filter_var($_SESSION['email'], FILTER_SANITIZE_STRING);
				$GET_USERS->execute();
				$GET_USERS->bind_result($GET_U_ID, $GET_U_FIRSTNAME, $GET_U_LASTNAME, $GET_U_EMAIL);

				while($GET_USERS->fetch()) {

					$GET_U_IDS = $GET_U_ID;
					$GET_U_FIRSTNAMES = $GET_U_FIRSTNAME;
					$GET_U_LASTNAMES = $GET_U_LASTNAME;
					$GET_U_EMAILS = $GET_U_EMAIL;

				}

				$this->id = $GET_U_IDS;
				$this->firstname = $GET_U_FIRSTNAMES;
				$this->lastname = $GET_U_LASTNAMES;
				$this->email = $GET_U_EMAILS;

				echo $this->id . ": " . ucfirst($this->firstname);
				if($this->lastname == "") {
				} else {

					echo " " . ucfirst($this->lastname);

				}

				echo " - " . $this->email;

			}

		} else {

			echo GUEST_EMAIL;

		}
	}
}
When you try out this code, you'll get
[text]: - [/text]
as the output. Nothing was put in there, but at the same time, the session email is set.

Can anyone help me figure this out?

Re: MVC won't show sessions

Posted: Thu Oct 02, 2014 12:44 pm
by requinix
...

You can't have a class named "New". It's a reserved word in PHP.

Re: MVC won't show sessions

Posted: Thu Oct 02, 2014 2:18 pm
by jacques1
requinix wrote:...

You can't have a class named "New". It's a reserved word in PHP.
No. The real application has a different name. This is exactly the same codes except the names were changed because it uses my test website's domain name. The application I made has a whole different name to it. The class name "New" is just for here. I know that there are some reserved words that I can't use, that's why I don't use them.

If you change "New" to something else like Gamecube or something. You'll see that the sessions don't show up even when the sessions are set. There isn't any problems with session being set because I don't get any errors even if I turn on error logging.

Re: MVC won't show sessions

Posted: Thu Oct 02, 2014 3:07 pm
by requinix
I brought that up so we could establish just how much code you changed before you posted it here. Fortunately it was just the class name.

Code: Select all

$GET_USERS->bind_param("s", $SESSION_EMAIL);
$SESSION_EMAIL = filter_var($_SESSION['email'], FILTER_SANITIZE_STRING);
Statements are out of order.

Re: MVC won't show sessions

Posted: Thu Oct 02, 2014 3:26 pm
by jacques1
requinix wrote:I brought that up so we could establish just how much code you changed before you posted it here. Fortunately it was just the class name.

Code: Select all

$GET_USERS->bind_param("s", $SESSION_EMAIL);
$SESSION_EMAIL = filter_var($_SESSION['email'], FILTER_SANITIZE_STRING);
Statements are out of order.
No. That shouldn't be the problem. I've always been using the statements in that order and nothing goes wrong. I've tested it again and I still get the same thing. No errors are displaying and no user credentials are too.

Hmm, but that's strange. When I try this on my localhost, it seems to work just fine. But it doesn't show anything on a live site. It's using the same PHP version so I'm not sure what's going on. Error logging is turned on, but I don't see any errors being displayed. Can this be because sessions aren't saved on the live server?

Re: MVC won't show sessions

Posted: Thu Oct 02, 2014 4:05 pm
by requinix
Ah, yes, I forgot that bind_param() works by-ref. IMO the statements should still be the other way around, but they'll work either way.

Everything else looks alright. Are you sure you actually have a test@test.com user in the database?

Re: MVC won't show sessions

Posted: Thu Oct 02, 2014 4:36 pm
by jacques1
requinix wrote:Ah, yes, I forgot that bind_param() works by-ref. IMO the statements should still be the other way around, but they'll work either way.

Everything else looks alright. Are you sure you actually have a test@test.com user in the database?
Right, I should of explained that as well before. The few things I've actually changed from here and the actual application is:
  1. The class names.
  2. The email.
  3. The guest email.
  4. The database credentials.
And that's about it. Everything from the actual application and the ones I've posted here are exactly the same. The email test@test.com doesn't exist in my database, but the email in the original application does exist in the database.

When I looked at it on my localhost, I used my login system to set the session using $_SESSION['email']; and it set the email from the database to the $_SESSION['email'];
Then I checked back on this application and it worked. But since I don't want to use my login system and I want to make a new login system, I set up a new application on a live server and it doesn't seem to be working.

On the live site, it doesn't seem to be fetching the user credentials because nothing is setting the $_SESSION['email'], but on my localhost it does because there is already a login system that uses the same $_SESSION variable.

Re: MVC won't show sessions

Posted: Thu Oct 02, 2014 4:47 pm
by jacques1
Wait, actually. I went back and looked at it because you mentioned if it exists in the database and I forgot that the email I was using doesn't exist in my database.

For the application, I used @hotmail.com and in the database, it was actually @gmail.com. That was the problem. Thanks for pointing it out for me. Now both live and local applications works. Thanks.