Questions tagged [php]
PHP is a widely-used, general-purpose scripting language that is especially suited for web development.
4,306
questions
5
votes
2
answers
666
views
Count each word within a string
I am looking for a solution with fewer lines of code. This is code that is working perfectly, but I think I typed too much for this algorithm. If I did something wrong, feel free to tell me the error.
...
7
votes
2
answers
490
views
OOP PHP form builder with validation
Yes, another PHP form builder... I wrote this solely to demonstrate OOP principles.
My questions are:
Could this be written better? OOP-wise.
How could I better implement error handling?
Index.php
<...
0
votes
1
answer
69
views
PHP assign values within in_array
What is a more efficient way of writing this sample code? Its purpose is to create a variable for days of the week based on a numerical value. This data is obtained from user input checkboxes. If the ...
5
votes
3
answers
135
views
DB Class extending PDO w/ other useful functions
I have this DB class that extends PDO, that also has useful functions for certain tasks; I was wondering how well it's coded, and if so, what I could maybe improve on. I tried commenting in ...
1
vote
1
answer
38
views
first try packagist: Detection of execution by command (Terminal) or web environment
I'm here again; I'm learning a bit about github and posting things to packagist, this is my first try.
I have created a library that validates or verifies in what type of environment php is being ...
-3
votes
2
answers
134
views
Insert employee data into a database
I've constructed code using prepared statements to try and avoid chances of SQL injections. This code is meant to insert the employees information. It does what I need it to do but I want to know if ...
2
votes
0
answers
56
views
Magic Class Invoker for procedural script
I have inherited a project in bad conditions that contains the following class and I need your help to improve it, the truth is I don't know if it is correctly optimized or if it has security problems ...
-1
votes
1
answer
31
views
Validation rules for payment cards in a data set
I have the following part of code. I dislike this, because if I need to add additional validation rule, I have to make changes in loop and create a varaible to store boolean result. For example check ...
1
vote
1
answer
187
views
Laravel 9 - Metadata creation using middleware?
I wrote a middleware for laravel that grabs the current metadata for the specific URL you visit.
How it works:
we use the $request and compare the current URL: <...
0
votes
0
answers
32
views
Execute a sequence of instructions
I wrote a quick POC application to execute a sequence of defined instructions and I would like some feedback if possible.
This is purely a proof of concept and I wanted to implement a concept similar ...
1
vote
1
answer
102
views
Sudoku puzzle solver for Leetcode
I just solved a problem from leetcode that is faster than 28.42% of the PHP codes, so the main review target is not the performance or alternative ways to solve this problem. I wrote the code thinking ...
-2
votes
1
answer
122
views
Insert row into database using static or non-static method [closed]
I would like to know if there are any benefits of one of the following 2 methods which insert an object into a database
The first function calls the Model method statically, creating a new instance of ...
1
vote
0
answers
62
views
php login system - how to make the login part better? [closed]
learned from a tutorial how to build it, but modified it's login part to fit me.
but, I'm not so sure it's the best login so I want to know how can I make it better.
the code:
index.php
...
0
votes
2
answers
274
views
Algorithm for twoSum
This is my PHP algorithm for the twoSum problem in leetCode:
...
3
votes
2
answers
193
views
Cryptographically secure version of the core array_rand() function
I want a cryptographically secure version of array_rand(). Is this it?
...
0
votes
1
answer
75
views
Verifying user login status
When a user logs in, I save a token (hashed password) to the user's folder on the server and send the token also to the app which is saved in localStorage.
I need to check if the user's login token ...
0
votes
2
answers
180
views
Version 2: Display visitor's number on your web page (changed code after getting answer for first version)
Version 2: Display visitor's number on your web page (changed code after getting answer for first version).
The first version is here: Display visitor's number on your web page
The "visitors&...
0
votes
1
answer
168
views
Display visitor's number on your web page
Display visitor's number on your web page.
The "visitors" table has only one column (visitor_count) and only one row. The column's initial value is 0.
I know goto should not be used but then ...
3
votes
1
answer
118
views
Proper PDO connection function
I am reviewing the database class for a live website. I am not sure if the current connect function is the best way to create a pdo object as it appears to create a new connection every time it is ...
0
votes
1
answer
56
views
Laravel controller getting results based on parameter including relationship results
I am trying to learn to improve my coding on a daily basis and this is one of the first times I am posting on here, so bear with me.
I have two models, a Payment and an Expense model. A ...
3
votes
1
answer
171
views
Optimization of laravel pivot table relationship
I have a pivot table called invite_riskarea which is designed as follows:
This table stores the permissions that have a specific user (through an invite id) to ...
1
vote
1
answer
70
views
Updating multiple database tables when products are shipped back by customer
So, in this example I have to make these changes in my tables:
Select Product Data => SKU, QTY
Insert into shipping_back table
Update in shipping_back to current date
Update in customers to ...
1
vote
0
answers
402
views
deprecated strftime in PHP 8.1
I saw that strftime is deprecated in PHP 8.1.
I saw I can use a class include in intl but I'm not sure to understand how to use It.
On my site I use this function with the array $datesToConvert{020222,...
1
vote
1
answer
60
views
PHP autoloader that uses array of file paths
After doing much reading of the PHP docs and trying out other developers' autoloader code, below is what I've come up with for an autoloader. It is working as expected so far. Posting the code here ...
0
votes
0
answers
60
views
Image file uploader webpage in HTML5 and PHP
I am trying to make a webpage for uploading image file and storing the uploaded file on server. There are several features in this implementation:
There is a drag & drop zone on the webpage. You ...
1
vote
0
answers
283
views
PayPal payment logic in AJAX/PHP/Laravel
I've just finished setting up the PayPal checkout integration for my Laravel web app. It successfully processes a transaction and persists it in my database, and I want to make sure something as ...
4
votes
2
answers
169
views
Modify a WordPress loop to show posts of certain categories, based on user roles
I've got some code that works as intended, but I have a feeling that it's messy. This code is modifying a wordpress loop and the goal is to check the roles of the current user and show posts of ...
0
votes
1
answer
68
views
Authenticate and login script updated
How can I improve/secure my login script and how to check for any possible injection?
PS. the script must run on multiple platforms, so I need empty arrays for cases such as the Android ones.
UPDATES
...
0
votes
1
answer
45
views
Authenticate and login script
How can I improve/secure my login script and how to check for any possible injection?
PS. the script must run on multiple platforms, so I need empty arrays for cases such as the Android ones.
...
1
vote
2
answers
79
views
Having trouble deleting multiple queries (PHP, MYSQL)
I'm having trouble making WHERE IN query work, I was only able to make it work as a foreach loop.
My payload looks like this:
sku[]: BOOK0002
sku[]: BOOK0003
My ...
1
vote
1
answer
59
views
Regex to capture possible US date entries
The Problem
I'm relying on plaintext entry to capture dates.
End users are urged to enter as MM-DD-YY, but it'll be great to account for as many possibilities.
US ...
-1
votes
1
answer
172
views
Refactoring this code into OOP & less functional programming
I got feedback from an interview test where they wanted me to prove that I've got a grasp of OOP php programming. This file especially I got feedback on that it's more functional than it is Object-...
1
vote
1
answer
122
views
Authenticate and log in users
How can I improve my login script and how to check for any possible injection?
PS. the script must run on multiple platforms, so I need empty arrays for cases such as the Android ones.
user_table:
<...
2
votes
1
answer
89
views
array_reduce() for calculating totals
Below is a snippet that uses array_reduce() to calculate totals from a detailed dataset. The data shown is representative of a larger dataset.
This code outputs ...
3
votes
1
answer
47
views
Retrieve config values using custom helper
I've stored a set of theme related configuration in a config file.
config/theme.php
...
3
votes
3
answers
204
views
Regex to extract portions of string about shipment
The Problem
I'll be extracting up to 4 parts of a String that come from user-input.
All 4 parts need to be in order i.e. (1-4) below in that order
The first capturing group is required and the last ...
0
votes
1
answer
95
views
Upgrade security and code quality of a website contact form
I have a simple website contact form created back in 2017.
The form was developed using PHP, PHPMailer, jQuery, HTML and CSS.
I would like to make sure the code is up to modern standards and secure.
I ...
1
vote
1
answer
46
views
0
votes
0
answers
49
views
Using configuration file to store classes
I'm using a configuration file to store tailwindcss classes. The idea is to make a dynamic component and simply call the class from configuration file base prop call on a component. eg:
in config/...
1
vote
1
answer
49
views
Search controller logic to search through models and manufacturers
I have the following Laravel controller that searches through two models - Manufacturer and SearchTerm (which contains models of manufacturers, synonyms of models as well as common misspellings) using ...
1
vote
1
answer
44
views
User controller with method to display user profiles
I have few questions. Sorry if title is not a good fit. I could not think of a good one. I have read online about maintainability and elegance of the code. I have also seen few examples of DRY ...
2
votes
2
answers
158
views
Is this a good way to process an order? (SQL+PHP)
I'm making a simple webshop, it's not the full code, there are more security procedures. I'm just want to know your opinions. Thank you.
These SQL tables I have:
(I have more column in this table this ...
4
votes
2
answers
803
views
using PHP and JS to fetch data for an entry
I need to fetch some data from a mysql database. For that I created get_data.php on the server and a js function to retrieve that data on the client. Is this the proper way to do it? What would you ...
0
votes
1
answer
147
views
Is this session manager secure?
I'm looking for best practices for writing secure session managers.
I'm making a table for the authorization token (UUID OR GUIDv4) with autoincrement, user_id, token, status (enum) then update status ...
1
vote
1
answer
113
views
Validate avatar image
I want to know if that code is correct and readable.
In class avatar the method getAvatar() validates the data. It checks if the field is not empty and if the photo extension is correct. If not, an ...
2
votes
2
answers
133
views
Optimize PHP code for Wordpress shortcode (Star-Rating)
I am using WP with the Avada theme and the built in Star Rating element as well as ACF Pro.
Unfortunately I found out that the Star-Rating element adds a rating scheme to the footer of the website ...
1
vote
4
answers
253
views
table name column name from GET parameter
This is a function that reads a setting data from DB.
...
0
votes
2
answers
80
views
Security of my upload script #2
I recently asked this question and got a response. I made improvements, it now checks if the file is an image and/or used UUID to store the images, and now I want to see if it's good or not.
How ...
2
votes
1
answer
192
views
PHP OOP: Configuring MySQL DB & query w/ Twig
I decided to up my PHP game and learn some OOP.
I am re-building my website from procedural to OOP, but since I don't want to use a full framework I fiddle with some components.
As a router I use ...
1
vote
1
answer
38
views
implode on two level dimensions array
I have an array like this
...