Jump to content

Explode (); Problem *Solved


Drezard

Recommended Posts

Hello, Heres my class with all my functions:

[CODE]
<?php
session_start();

error_reporting(E_ALL);

class web {

var $connection = NULL;
var $login = NULL;
var $dbhost = "localhost";
  var $dbuser = "test";
  var $dbpass = "test";
  var $db = "sellyourownhome";

function connect() {

$this->connection = mysql_connect($this->dbhost, $this->dbuser, $this->dbpass) or die ("Unable to connect!");
   
    mysql_select_db($this->db) or die ("Unable to select database!");

}

function close_connect() {

mysql_close($this->connection);

}

function check_user() {
 
    if (!isset($_SESSION['userinfo'])) {
 
      $this->login_box();
     
    }
   
    if (isset($_SESSION['userinfo'])) {
 
      $this->connect();

$userinformation = explode(" ", $_SESSION['userinfo']);
$user = $userinformation[0];
$pass = $userinformation[1];

echo $user;
echo $pass;

$query = "SELECT * FROM users WHERE user='$user' AND pass='$pass'";

$result = mysql_query($query) or die ("Error in query: $query. ".mysql_error());

$count = mysql_num_rows($result);

if ($count == 1) {

$this->login = 1;

}

if ($count == 0) {

echo "ERROR: Session has corrupted. Please logout and relogin";

$this->login = 0;

}

 
    }
 
  }

function add_user() {

    if (isset($SESSION['userinfo'])) {
   
      $this->check_user();
   
    }
   
    if (!isset($_SESSION['userinfo'])) {
   
  $this->connect();

if (!isset($_POST['user'])) {

echo "<form action='' method='post'>
    Username: <input type='text' name='user'>
    Password: <input type='password' name='pass'>
Comfirm Password: <input type='password' name='pass2'>
Email: <input type='text' name='email'>
Comfirm Email: <input type='text' name='email2'>
    <input type='submit' name='submit'>
    </form>";

}

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

$user = $_POST['user'];
$pass = $_POST['pass'];
$pass2 = $_POST['pass2'];
$email = $_POST['email'];
$email2 = $_POST['email2'];
$priv = $_POST['priv'];

if ($pass == $pass2) {

if ($email == $email2) {

              $query = "SELECT * FROM users WHERE user='$user' AND pass='$pass'";

        $result = mysql_query($query) or die ("Error in query: $query. ".mysql_error());

        $count = mysql_num_rows($result);

        if ($count == 1) {

          echo "Username already created";

          }

        if ($count == 0) {

     
    $query = "INSERT INTO users (user, pass, priv) VALUES ('$user', '$pass', '2')";

    $result = mysql_query($query) or die ("Error in query: $query.");

    $query = "INSERT INTO profiles (user, email) VALUES ('$user', '$email')";

    $result = mysql_query($query) or die ("Error in query: $query.");

    echo "Acount sucessfully created";

        }

}

else {

echo "Emails dont match";

}

}

else {

echo "Passwords dont match";

}

    }
       
      }
       
    }

function login_box() {

$this->connect();

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

$this->check_user();

$userinformation = explode(" ", $_SESSION['userinfo']);
$user = $userinformation[0];

echo 'Welcome, $user';

}

if (!isset($_SESSION['userinfo']) && !isset($_POST['user'])) {

echo "<form action='' method='post'>
    Username: <input type='text' name='user'>
    Password: <input type='password' name='pass'>
    <input type='submit' name='submit'>
    </form>";

}

if (!isset($_SESSION['userinfo']) && isset($_POST['user'])) {

$user = $_POST['user'];
$pass = $_POST['pass'];

$query = "SELECT * FROM users WHERE user='$user' AND pass='$pass'";

$result = mysql_query($query) or die ("Error in query: $query. ".mysql_error());

$count = mysql_num_rows($result);

if ($count == 1) {

$_SESSION['userinfo'] = '$user $pass';

echo "Sucessfully logged in";

}

if ($count == 0) {

echo "Username or password are incorrect.";

}

}

}

function logout() {

if (!isset($_SESSION['userinfo'])) {

echo "You have to be logged in to view this.";

}

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

session_destroy();

echo "You have sucessfully logged out";

}

}
 
}




?>
[/CODE]

Alright so with the function check_user(); it doesnt explode the session properly. The variables $user and $pass are empty.

Whats wrong with it?

The whole code is there so you should be able to see that the function login_box sets the session and i want check_user to read it but it doesnt.

- Cheers, Daniel
Link to comment
https://forums.phpfreaks.com/topic/30018-explode-problem-solved/
Share on other sites

A few things to try, first off, I am pretty sure if you do it the way you are, you need it to look like
$_SESSION['userinfo'] = $user . " " . $pass; // Took out quotes and added a period which binds them together with the space
Could have two variables if that does not work, a user and pass variable
Tell me what happens if you do the period one though, that should help you in what you need

Archived

This topic is now archived and is closed to further replies.

×
×
  • 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.