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
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
Link to comment
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.