1

I'm a new in php, I have a sign up form and I want to store users registered in an array or JSON, I built user class and when I register a new user I want to add it into this array or JSON, but session array accept only one user in it and when I adding new user session removing the old one and store the new one! This is my code:

class User
{
        private $id;
        private $first_name;
        private $last_name;
        private $email;
        private $password;
    
        public function register($id, $firstName, $lastName, $email, $password)
        {
            $this->id = $id;
            $this->first_name = stripslashes($firstName);
            $this->last_name = stripslashes($lastName);
            $this->email = $email;
            $this->password = password_hash($password, PASSWORD_DEFAULT);
        }
    }



class DB
{
    public $users;

    public function __construct()
    {
        $this->users = [];
    }
}


<?php
$counter = 0;
$_SESSION['usersDB'] = new DB;

if (isset($_POST['submit'])) {
    $firstName = $_POST['firstName'];
    $lastName = $_POST['lastName'];
    $email = $_POST['email'];
    $password = $_POST['password'];
    $user = new User;
    $user->register(++$counter, $firstName, $lastName, $email, $password);
    array_push($_SESSION['usersDB']->users, $user);
}
echo '<pre>';
var_dump($_SESSION['usersDB']);
echo '</pre>';
?>

What I should do to sole this and store all users in one place?

3 Answers3

1

You're replacing the session variable with new DB each time you run the script. You shouldn't do that if the session variable is already set.

if (!isset($_SESSION['userdDB'])) {
    $_SESSION['usersDB'] = new DB;
}

Also, $counter will always be 1, since you're setting $counter = 0; at the beginning of the script. You could save this in a session variable, but there isn't really a need. You can just use:

$counter = count($_SESSION['usersDB']->users);

I'm not really sure this will do what you really want. Every browser session has its own session variables, so each user will just have a list of users that they have registered. Session variables are also temporary, so it's not a good way to keep a permanent list of registered users.

The right way to keep a permanent list of users is in a database on the server.

Barmar
  • 741,623
  • 53
  • 500
  • 612
0

using cookies with serialize and unserialize function

user.php

<?php
class User
{
    public static $cnt = 0;
    private $id;
    private $name;
    public function __construct($name='')
    {
        self::$cnt++;
        $this->id = self::$cnt;
        $this->name = stripslashes($name);
    }
    public function __get($name){
        return $this->$name;
    }
    public function __set($name,$val){
        $this->$name = stripslashes($val);
    } 

    public function __toString(){
        return 'user('.$this->id.", ".$this->name.")";
    }
}
?>

db.php

<?php
class DB
{
    public $users = [];
    public function __construct()
    {
        $this->users = [];
    }

    public function __toString()
    {
        $str = "<ul>";
        foreach ($this->users as $user)
            $str .="<li>".$user."</li>";
        $str .= "</ul>";
        return $str;
    }
}
?>

index.php

<?php
require_once('user.php');

$user1 = new User('Steve');
$user2 = new User('Everst');

require_once('db.php');
$databse = new DB();

$databse->users[] = $user1;
$databse->users[] = $user2;

setcookie('users', serialize($databse),time() + 3600,"/","",0);
echo $_COOKIE['users'];
?>

users.php

<?php
require_once('db.php');
require_once('user.php');
$databse = unserialize($_COOKIE['users']);
echo $databse;
?>
XMehdi01
  • 5,538
  • 2
  • 10
  • 34
0

using session with JSON

  1. implements the interface JsonSerializable
  2. override the method jsonSerialize

user.php

<?php
class User implements JsonSerializable
{
    public static $cnt = 0;
    private $id;
    private $name;
    public function __construct($name='')
    {
        self::$cnt++;
        $this->id = self::$cnt;
        $this->name = stripslashes($name);
    }
    public function __get($name){
        return $this->$name;
    }
    public function __set($name,$val){
        $this->$name = stripslashes($val);
    } 

    public function __toString(){
        return 'user('.$this->id.", ".$this->name.")";
    }

    public function jsonSerialize() {
        return array(
             'id' => $this->id,
             'name' => $this->name
        );
    }
}
?>

index.php

<?php
session_start();

include('user.php');
include('db.php');

$user1 = new User('Steve');
$user2 = new User('Everst');
$databse = new DB();
$databse->users[] = $user1;
$databse->users[] = $user2;

$_SESSION['database'] = JSON_encode($databse);//{"users":[{"id":1,"name":"Steve"},{"id":2,"name":"Everst"}]}

?>

users.php

<?php
session_start();
$databse = json_decode($_SESSION['database']);
foreach ($databse->users as $user)
    echo $user->id." - ".$user->name."<BR>";
?>
XMehdi01
  • 5,538
  • 2
  • 10
  • 34