Questions tagged [php]
PHP is a widely used, open source, general-purpose, multi-paradigm, dynamically typed and interpreted scripting language originally designed for server-side web development. Use this tag for questions about programming in the PHP language.
107,099
questions
2773
votes
28
answers
2.0m
views
How can I prevent SQL injection in PHP?
If user input is inserted without modification into an SQL query, then the application becomes vulnerable to SQL injection, like in the following example:
$unsafe_variable = $_POST['user_input'];
...
1327
votes
29
answers
2.1m
views
"Notice: Undefined variable", "Notice: Undefined index", "Warning: Undefined array key", and "Notice: Undefined offset" using PHP
I'm running a PHP script and continue to receive errors like:
Notice: Undefined variable: my_variable_name in C:\wamp\www\mypath\index.php on line 10
Notice: Undefined index: my_index C:\wamp\www\...
2657
votes
14
answers
247k
views
Why shouldn't I use mysql_* functions in PHP?
What are the technical reasons for why one shouldn't use mysql_* functions? (e.g. mysql_query(), mysql_connect() or mysql_real_escape_string())?
Why should I use something else even if they work on ...
745
votes
21
answers
774k
views
PHP parse/syntax errors; and how to solve them
Everyone runs into syntax errors. Even experienced programmers make typos. For newcomers, it's just part of the learning process. However, it's often easy to interpret error messages such as:
PHP ...
1241
votes
37
answers
256k
views
Reference - What does this error mean in PHP?
What is this?
This is a number of answers about warnings, errors, and notices you might encounter while programming PHP and have no clue how to fix them. This is also a Community Wiki, so everyone is ...
541
votes
30
answers
478k
views
PHP mail function doesn't complete sending of e-mail
<?php
$name = $_POST['name'];
$email = $_POST['email'];
$message = $_POST['message'];
$from = 'From: yoursite.com';
$to = 'contact@yoursite.com';
$subject = 'Customer ...
831
votes
11
answers
1.8m
views
How to fix "Headers already sent" error in PHP
When running my script, I am getting several errors like this:
Warning: Cannot modify header information - headers already sent by (output started at /some/file.php:12) in /some/file.php on line 23
...
1306
votes
13
answers
187k
views
UTF-8 all the way through
I'm setting up a new server and want to support UTF-8 fully in my web application. I have tried this in the past on existing servers and always seem to end up having to fall back to ISO-8859-1.
Where ...
2250
votes
31
answers
457k
views
How do you parse and process HTML/XML in PHP?
How can one parse HTML/XML and extract information from it?
146
votes
1
answer
63k
views
How to deal with mysqli problems? mysqli_fetch_array(): Argument #1 must be of type mysqli_result
In my local/development environment, the MySQLi query is performing OK. However, when I upload it on my web host environment, I get this error:
Fatal error: Call to a member function bind_param() on ...
742
votes
4
answers
289k
views
SQL injection that gets around mysql_real_escape_string()
Is there an SQL injection possibility even when using mysql_real_escape_string() function?
Consider this sample situation. SQL is constructed in PHP like this:
$login = mysql_real_escape_string(...
552
votes
4
answers
51k
views
What is the difference between client-side and server-side programming?
I have this code:
<script type="text/javascript">
var foo = 'bar';
<?php
file_put_contents('foo.txt', ' + foo + ');
?>
var baz = <?php echo 42; ?>;
...
268
votes
1
answer
408k
views
How to extract and access data from JSON with PHP?
This is intended to be a general reference question and answer covering many of the never-ending "How do I access data in my JSON?" questions. It is here to handle the broad basics of decoding JSON in ...
391
votes
35
answers
808k
views
PHP code is not being executed, but the code shows in the browser source code
I'm trying to execute some PHP code on a project (using Dreamweaver) but the code isn't being run.
When I check the source code, the PHP code appears as HTML tags (I can see it in the source code). ...
994
votes
31
answers
772k
views
mysql_fetch_array()/mysql_fetch_assoc()/mysql_fetch_row()/mysql_num_rows etc... expects parameter 1 to be resource
I am trying to select data from a MySQL table, but I get one of the following error messages:
mysql_fetch_array() expects parameter 1 to be resource, boolean given
This is my code:
$username = $...
365
votes
17
answers
622k
views
Convert one date format into another in PHP
Is there a simple way to convert one date format into another date format in PHP?
I have this:
$old_date = date('y-m-d-h-i-s'); // works
$middle = strtotime($old_date); // ...
4932
votes
23
answers
780k
views
Reference — What does this symbol mean in PHP?
What is this?
This is a collection of questions that come up every now and then about syntax in PHP. This is also a Community Wiki, so everyone is invited to participate in maintaining this list.
Why ...
1908
votes
31
answers
3.3m
views
How do I get PHP errors to display?
I have checked my PHP ini file (php.ini) and display_errors is set and also error reporting is E_ALL. I have restarted my Apache webserver.
I have even put these lines at the top of my script, and it ...
113
votes
4
answers
16k
views
Can I mix MySQL APIs in PHP?
I have searched the net and so far what I have seen is that you can use mysql_ and mysqli_ together meaning:
<?php
$con=mysqli_connect("localhost", "root" ,"" ,"mysql");
if( mysqli_connect_errno( ...
632
votes
41
answers
659k
views
How can I get useful error messages in PHP?
Quite often I will try and run a PHP script and just get a blank screen back. No error message; just an empty screen. The cause might have been a simple syntax error (wrong bracket, missing semicolon),...
890
votes
7
answers
437k
views
What is the difference between single-quoted and double-quoted strings in PHP?
I'm a little confused why I see some code in PHP with string placed in single quotes and sometimes in double quotes.
I just know in .NET, or the C language, if it is in a single quote, that means it ...
175
votes
3
answers
56k
views
Reference: What is variable scope, which variables are accessible from where and what are "undefined variable" errors?
Note: This is a reference question for dealing with variable scope in PHP. Please close any of the many questions fitting this pattern as a duplicate of this one.
What is "variable scope" in PHP? Are ...
747
votes
19
answers
946k
views
How do I pass variables and data from PHP to JavaScript?
I have a variable in PHP, and I need its value in my JavaScript code. How can I get my variable from PHP to JavaScript?
I have code that looks like this:
<?php
$val = $myService->getValue(); // ...
319
votes
13
answers
86k
views
How can I sort arrays and data in PHP?
This question is intended as a reference for questions about sorting arrays in PHP. It is easy to think that your particular case is unique and worthy of a new question, but most are actually minor ...
50
votes
1
answer
16k
views
Why does this PDO statement silently fail?
This is my PHP SQL statement and it's returning false while var dumping
$sql = $dbh->prepare('INSERT INTO users(full_name, e_mail, username, password) VALUES (:fullname, :email, :username, :...
157
votes
5
answers
48k
views
The 3 different equals
What is the difference between =, ==, and ===?
I think using one equal sign is to declare a variable while two equal signs are for a comparison condition and lastly three equal signs are for ...
1319
votes
11
answers
453k
views
How do you use bcrypt for hashing passwords in PHP? [duplicate]
Every now and then I hear the advice "Use bcrypt for storing passwords in PHP, bcrypt rules".
But what is bcrypt? PHP doesn't offer any such functions, Wikipedia babbles about a file-encryption ...
1233
votes
14
answers
250k
views
Secure hash and salt for PHP passwords
It is currently said that MD5 is partially unsafe. Taking this into consideration, I'd like to know which mechanism to use for password protection.
This question, Is “double hashing” a password less ...
102
votes
2
answers
7k
views
Cleansing User Passwords
How should I escape or cleanse user-provided passwords before I hash them and store them in my database?
When PHP developers consider hashing users' passwords for security purposes, they often tend ...
1462
votes
33
answers
3.4m
views
How do I make a redirect in PHP?
Is it possible to redirect a user to a different page through the use of PHP?
Say the user goes to www.example.com/page.php and I want to redirect them to www.example.com/index.php, how would I do so ...
80
votes
6
answers
118k
views
How can I access an array/object?
I have the following array and when I do print_r(array_values($get_user));, I get:
Array (
[0] => 10499478683521864
[1] => 07/22/1983
[2] => email@saya.com
...
781
votes
35
answers
1.0m
views
How to calculate the difference between two dates using PHP?
I have two dates of the form:
Start Date: 2007-03-24
End Date: 2009-06-26
Now I need to find the difference between these two in the following form:
2 years, 3 months and 2 days
How can I do this ...
261
votes
8
answers
93k
views
Can PHP PDO Statements accept the table or column name as parameter?
Why can't I pass the table name to a prepared PDO statement?
$stmt = $dbh->prepare('SELECT * FROM :table WHERE 1');
if ($stmt->execute(array(':table' => 'users'))) {
var_dump($stmt->...
148
votes
4
answers
73k
views
How can I convert ereg expressions to preg in PHP?
Since POSIX regular expressions (ereg) are deprecated since PHP 5.3.0, I'd like to know an easy way to convert the old expressions to PCRE (Perl Compatible Regular Expressions) (preg).
Per example, I ...
72
votes
5
answers
414k
views
How to include a PHP variable inside a MySQL statement
I'm trying to insert values in the contents table. It works fine if I do not have a PHP variable inside VALUES. When I put the variable $type inside VALUES then this doesn't work. What am I doing ...
1332
votes
16
answers
1.0m
views
How to Sort a Multi-dimensional Array by Value
How can I sort this array by the value of the "order" key?
Even though the values are currently sequential, they will not always be.
Array
(
[0] => Array
(
[...
128
votes
6
answers
250k
views
How to use PHP's password_hash to hash and verify passwords
Recently I have been trying to implement my own security on a log in script I stumbled upon on the internet. After struggling of trying to learn how to make my own script to generate a salt for each ...
211
votes
10
answers
705k
views
PHP - Failed to open stream : No such file or directory
In PHP scripts, whether calling include(), require(), fopen(), or their derivatives such as include_once, require_once, or even, move_uploaded_file(), one often runs into an error or warning:
...
1234
votes
17
answers
627k
views
How can I sanitize user input with PHP?
Is there a catchall function somewhere that works well for sanitizing user input for SQL injection and XSS attacks, while still allowing certain types of HTML tags?
1143
votes
17
answers
1.1m
views
How do I expire a PHP session after 30 minutes?
I need to keep a session alive for 30 minutes and then destroy it.
474
votes
16
answers
964k
views
How can I parse a JSON file with PHP? [duplicate]
I tried to parse a JSON file using PHP. But I am stuck now.
This is the content of my JSON file:
{
"John": {
"status":"Wait"
},
"Jennifer": {...
170
votes
16
answers
729k
views
How do I pass JavaScript variables to PHP?
I want to pass JavaScript variables to PHP using a hidden input in a form.
But I can't get the value of $_POST['hidden1'] into $salarieid. Is there something wrong?
Here is the code:
<script ...
177
votes
18
answers
624k
views
How can I combine two strings together in PHP?
I don't actually know how to describe what I wanted, but I'll show you:
For example:
$data1 = "the color is";
$data2 = "red";
What should I do (or process) so $result is the ...
612
votes
22
answers
235k
views
Can I bind an array to an IN() condition in a PDO query?
I'm curious to know if it's possible to bind an array of values to a placeholder using PDO. The use case here is attempting to pass an array of values for use with an IN() condition.
I'd like to ...
570
votes
5
answers
194k
views
How should a model be structured in MVC? [closed]
I am just getting a grasp on the MVC framework and I often wonder how much code should go in the model. I tend to have a data access class that has methods like this:
public function CheckUsername($...
573
votes
23
answers
1.2m
views
Change the maximum upload file size
I have a website hosted on a PC I have no access to. I have an upload form allowing people to upload mp3 files up to 30MB big. My server side script is done in PHP.
Every time I try and upload a file,...
34
votes
4
answers
52k
views
mysqli or die, does it have to die?
If I use a bit of code like this:
$update_result = mysqli_query( $link , $sql_update_login ) or die ('Unable to execute query. '. mysqli_error($link));
Does it have to die or can you put a ...
342
votes
31
answers
348k
views
How to Flatten a Multidimensional Array?
Is it possible, in PHP, to flatten a (bi/multi)dimensional array without using recursion or references?
I'm only interested in the values so the keys can be ignored, I'm thinking in the lines of ...
130
votes
0
answers
143k
views
PHP's white screen of death [duplicate]
Now that I'm starting to get back into PHP, I'm starting to remember why I gave it up in the first place. The most annoying thing on my plate at the moment is what I've come to term "PHP's white ...
535
votes
28
answers
118k
views
Are PHP short tags acceptable to use?
Here's the information according to the official documentation:
There are four different pairs of
opening and closing tags which can be
used in PHP. Two of those, <?php ?>
and <...