20000 forum users - Awesome Special Event!

Why does PHP make me do all that :sob: Iā€™ve literally never had to do that before. (I also have like no clue how to work with classes)

Why though?

5 Likes

I believe it has to do with the fact that some other external function ends up accessing the same variable outside its scope for an unintended purpose which is what you dont want to happen.

It is quite easy to get started with, depending on what level you are:
https://www.php.net/manual/en/language.oop5.basic.php


It is going to be 5 years since I started using PHP and Javascript, I could technically apply for certain jobs which require at least a few years of experience šŸ™ƒ
7 Likes

I donā€™t know if it is just me but the PHP manual is very confusing to look at.

However, I was able to figure it out!

I donā€™t know if itā€™s the most efficient way to do this but if you guys see anything wrong with it, please let me know. Also thank you for your help Admin.

5 Likes

I see no reason why try to pass the arguments to the every method (read: functions of a class)
You need to define the constructor method:

function __construct($mysql, $login) {
        this->mysql = $mysql;
        this->login = $login;
    }

And then pass the parameters to the class:

$usrRstrcts = new UserRestrictions($mysql, $login);

And in order to call a method of it:

$usrRstrcts->banned();

On a side note: the code that you have done is vulnerable to SQL injections, take care of it using the prepared statements.

7 Likes

Yeah, I was thinking about that. My only problem with that is that using prepared statements adds a lot more lines to my code.


I donā€™t know if I did this right.

4 Likes

Sarcificing security for the sake of simplification is a bad idea, your choice!

6 Likes

Donā€™t worry, I only didnā€™t use prepared statements on this because I was lazy. For everything else, I have used prepared statements.

4 Likes

You can also use the mysqli::real_escape_string() function with $login as parameter if you really want to not add more lines to your code; the documentation is this:

https://www.php.net/manual/en/mysqli.real-escape-string.php

5 Likes

I just realized I put get_result() twice.


There we go

ā€¦ I didnā€™t change query to prepare.

5 Likes

I would prefer to execute the whole query in a separate method in order to prevent repeating it in all of the other methods.

Define the variable:

private $restrictions;

And put the lines regarding fetching data from the database inside the constructor method (below anything else in it) (by those lines I mean the first 5 lines of the banned method for example) and put the results inside the variable above:

$this->restrictions = $result->fetch_assoc();
6 Likes

You must be the PHP OOP classes master, thank you very much for your help!

6 Likes

You are welcome :slight_smile:

5 Likes

No problem :smile:

Iā€™m also glad to help you out with that little I gave if I helped at all!

4 Likes

My site isnā€™t redirecting to https although I have .htaccess. How do I fix it?

4 Likes

Whatā€™s your .htaccess, and whatā€™s your site?

4 Likes


Is it redirecting for you :thinking:

2 Likes

@ChrisPAR the day must have been busy af.

6 Likes

It is redirecting me, yes.

Indeedā€¦

5 Likes

EDIT: itā€™s still not redirected to https on my side for some reason

5 Likes

I checked and seems like the .htaccess isnā€™t being taken in considerationā€¦ Can you try using the snippet from this article instead?

Oh, also make sure itā€™s uploaded properly.

6 Likes