News Feed
  • DrugHub has agreed to fully refund all users who lost money in the SuperMarket exit scam.  
  • Retro Market has gone offline. Circumstances of the closure unknown.  
  • SuperMarket has closed following an exit scam by one of the admins.  
  • The admin of Incognito Market, Pharoah, has been arrested by the FBI several months after exit scamming.  
  • Silk RoadTorhoo mini logo
  • darknet markets list
  • Popular P2P exchange LocalMonero has announced it is closing.  

vanilla PHP question : programming | Torhoo darknet markets

currently working on a home grown vanilla PHP project and I am using
array_push()
to push a string to and empty array for all my user generated error messages. Then to display them i use
if(in_array($msg, $err_array)) echo $msg


Its becoming so clunky and really making readabilty difficult is there a better solution? Ive kind of tried to make a class but i fail somewhere and really just cant figure it out. I would like to have two functions one to push the error to array and one to display it. Any thoughts?
/u/c00laid
2 points
4 years ago*
Controller/ExampleController.php
<?php

class ErrHandling {
    public $ErrMsgs;

    public function __construct() {
        $this->ErrMsgs = NULL;        
    }
    public function PushMsgArr(&...$msgs) 
    {
        foreach ($msgs as $msg)
            array_push($this->ErrMsgs, htmlspecialchars($msg, ENT_QUOTES, 'UTF-8'));
    }
}
----------------------------------------------------------------------------------------------------- Views/ExampleView.php
<!DOCTYPE HTML>
<html>
<head>
    <meta charset = "utf-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <meta name = "robots" content = "noindex, nofollow">
    <title>Test Page Dread Error Handling</title>
</head>
<body>
    <?php
        require('../Controllers/ExampleErrControler.php') 
        $ErrHandler = new ErrHandling();
        // Do Shit here    
        if (Whateverhappens == wrong)
            $ErrHandler->PushMsgArr('We fucked up');
        
        if ($ErrHandler->ErrMsgs !== NULL) {
            foreach ($Errhandler->ErrMsgs as $ErrMsg)   
                echo '<p>$ErrMsg</p>';
        }
    ?>
</body>
</html>
Something like this, yes i know this is not really a MVC style really you'd have to use a framework for this or create one and manage all routes etc but something along the lines of this. You could use laravel and do something like ------------------------------------------------------------------------------------------------------------------- USING LARAVEL routes/web.php
<?php

use Illuminate\Support\Facades\Route;
use App\Http\Controllers\LoginController;

Route::get('/ExamplePage', function() {
    return view('ExamplePage');
};

Route::post('/Login', LoginController::class)->middleware('CheckReqParametersLoginPage');
Recourses/views/ExamplePage.blade.php
<!DOCTYPE html>
<html>
<head>
    <meta charset = "utf-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <meta name = "robots" content = "noindex, nofollow">
    <title>Test Page Dread Error Handling</title>
</head>
<body>  
    @php 
        @if ($errors->any())
            @foreach ($errors->all() as $ErrMsg)
                <p>${{ $ErrMsg }}</p>
            @endforeach
        @endif
    @endphp
    <form action = "/Login" method = "post">
        <input type = "text" value = "Username" placeholder = "Username">
        <input type = "password" value = "Password" placeholder = "Password">
    </form>
</body>
</html>
App/middleware/CheckReqParamatersLoginPage.php
<?php

namespace App\Http\Middleware

use Closure;

class CheckReqParamatersExamplePage
{
    public function handle($request, Closure $next) 
    {
        if (!$request->isMethod('post'))
            $Errors = array_push('Not allowed');
        if (!$request->has('Username') || !$request->has('Password'))
            $Errors = array_push('Not allowed');

        return $next($request, $CurrErrors);    
    }
}
Make sure to register the middleware app/Http/kernel.php You will see at the bottm a array routeMiddelware add array key / value in there
    'CheckReqParamatersLoginPage' => App\Http\Middleware\CheckReqParamatersLoginPage;
App/Http/Controllers/Logincontroller.php
<?php
namespace App\Http\Controllers;

use App\Http\Controllers\Controller;
use Illuminate\Http\Request;

class LoginController extends Controller
{
    public function LoginUser(Request $request, $ErrMsgs) 
    {
        // Do your shit here
        // if user doesn't exist whatever
            $Errors = array_push('User doesn't exist');
        if ($Errors !== NULL)
            return redirect()->back()->withErrors($ErrMsgs);
    }
}



Sorry for errors / typo's if there are any i didn't check just wrote this real quick without testing.

If you need any more help reply down below :p thx
/u/defnotspez 📢
1 points
4 years ago
Damn alright. Ill digest that. So that first bit of code is how you would create a class for it with a framework? I would personally rather just create a class for it and just be able to create a new instance and access the methods for array_push in the class and just have to put the string in the method thats all.
/u/c00laid
1 points
4 years ago
First example is just a class which deals with the errors. I recommend using frameworks or writing your own because they tend to work MVC style (Model, view, controller).

This keeps interacting with the database, the logic and the views (frontend). Separated it's annoying to use at first but it makes things more easy / faster to write and more oversee able.

Read the top two blocks of code everything below that uses the framework laravel
/u/Buckarooni
1 points
4 years ago
I guess this is the prolific stack overflow user in me, but I'd need to know much more or have a more pointed, directed goal to recommend a better alternative, but having done PHP for a long time, this seems perfectly appropriate to me.
/u/reply2
0 points
4 years ago
PHP is a very clunky and shitty language. My advice is to STOP USING IT. it's fucking 2021 the year of our lord.
/u/floatingpoint
1 points
4 years ago
PHP for backend kind-of sexy though...
/u/floatingpoint
1 points
4 years ago
jokes, its pretty junk. But useful junk none the less.
/u/defnotspez 📢
1 points
4 years ago
Yes i likie!
/u/defnotspez 📢
0 points
4 years ago
Can you elaborate? I like it.
/u/reply2
1 points
4 years ago
https://webonastick.com/php.html
https://eev.ee/blog/2012/04/09/php-a-fractal-of-bad-design/

Others have said it better than me.