PHP

Problems, solutions and everything in between.
Latest entries

Blocked by CORS policy: No ‘Access-Control-Allow-Origin’ header is present

If you're seeing the following error "has been blocked by CORS policy: No 'Access-Control-Allow-Origin' header is present on the requested resource." add the following into responding request. <?php header('Access-Control-Allow-Origin: *'); ?> There's alternative methods such as chrome extensions too.

What Are Laravel’s File/Folder Permissions When Installing?

Set the Permissions chown -R www-data:www-data /var/www/domain/ chmod -R 755 /var/www/domain/storage

CodeIgniter (Expression Engine Database Class) Get Single Value From Select Statement

Find more on CodeIgniter Active Class here and more on Expression Engine Database Class here. $results = ee()->db->order_by('id') ->limit(1) ->get('col_name'); $col_name = $results->row('col_name');

Artisan missing from Laravel project - Could not open input file: artisan

If you're receiving the error "Could not open input file: artisan" or words to the same effect, you may be missing the Artisan file altogether. Navigate to your project in terminal and run a cURL on the raw Artisan file. curl -L 'https://raw.githubusercontent.com/laravel/laravel/v5.5.0/artisan' > artisan This will pull the Artisan script from GitHub to your local project. Note: where I've used version 5.5.0 you will need to change the tag version

Setting up and working with DotEnv - PHP Environment Variables

Get started by installing composer. Then providing how you've installed it (with an alias or not you will need to run one of two commands) php composer.phar require vlucas/phpdotenv or composer require vlucas/phpdotenv This should install the necessary files and folders. Depending on where you want to save your vendor folder may change the location of the following lines, however I like to keep mine above public_html. $loc = dirname($_SERVER['DOCUMENT_ROOT']); // Declare a var directing the user…

Could not open input file: composer.phar

If you're having the above issue run the following command. curl -sS https://getcomposer.org/installer | php mv composer.phar /usr/local/bin/composer Then set up an alias alias composer='/usr/local/bin/composer.phar' You can then run the command as "composer" To check that it's working run composer --version You can require files as composer require Example composer require vlucas/phpdotenv As opposed to php composer.phar require vlucas/phpdotenv

[SOLVED] - PDO Message: Malformed UTF-8 characters, possibly incorrectly encoded

If you're seeing the below error when working with PDO, I recently run into the same issue, this was down to the charset on the initiation of PDO. Message: Malformed UTF-8 characters, possibly incorrectly encoded Adding in charset=utf8 should solve this issue. 'mysql:host=' . $this->host . ';dbname=' . $this->dbname . ";charset=utf8"

[SOLVED] - Return the path above httpdocs/public_html - one before DOCUMENT_ROOT

If you're after returning the path before public_html or httpdocs like so /home/domain Rather than /home/domain/public_html Use DOCUMENT_ROOT in conjunction with dirname(); dirname($_SERVER["DOCUMENT_ROOT"]) // returns /home/domain

Check if allow_url_fopen is open within PHP

var_dump(ini_get('allow_url_fopen')); You're expecting a boolean response, true/false 1/0

What is the difference between mail() and @mail() ?

mail() will execute and show any errors such as syntax, parse errors etc @mail will also execute, however it will mask any errors

PDO connect, select & insert blank template

$host = "localhost"; $user = 'user'; $password = 'pass'; $dbname = 'dbname'; try { $conn = new PDO("mysql:host=$host;dbname=$dbname;charset=utf8", $user, $password); // set the PDO error mode to exception $conn->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION); return $conn; } catch(PDOException $e) { exit("Connection failed: " . $e->getMessage()); } $stmt = $this->db->prepare("SELECT * FROM `users` WHERE `user_id` = :id ORDER BY `user_id` DESC LIMIT 1"); $stmt->bindValue(':id', $id);…

How to find your php.ini file

To find your php.ini file you need to first look at your PHPInfo page. Navigate to your public_html folder and create a new file with the below code. For argument purposes, let's call this file info.php. Navigate to info.php in your browser, from here you can simply search for "php.ini" and below the "Environment" section you'll see "PP_CUSTOM_PHP_INI" with the location to your custom php.ini file. You can also check "Loaded Configuration File" which will show you the actual loaded configuration…

Remove items from an array after a certain point with array_splice

To remove items after a certain point you should be using array_splice. $name = array("Chris", "Danny", "Benjamin", "Keith"); $cut = array_splice($name, 2); var_dump($cut); // outputs is an array ("Chris", "Danny") as we're using the first 2 items Read up on array_splice here : http://php.net/manual/en/function.array-splice.php

How to remove trailing slash from URL with PHP

To remove a character from the end of a string (in this case a URL) rtrim is the answer, which stands for "Right Trim". $link = rtrim($link,"/"); You can read up on rtrim on the PHP manual - http://php.net/rtrim

Return the first or last element in an array without deleting it

Returning the last item To return the last item in an array you can simply call the "end" function below. $array = array( 'a','b','c'); echo end($array); // will return "c" var_dump($array); More on the PHP function end() here. Return the first item Just like the end() function, we have available to us the "current()" function. $array = array( 'a','b','c'); echo current($array); // will return "a" var_dump($array); More on the PHP function current() here.

Show user information such as IP address and their browser

Showing users information is particularly easy, below is a list of useful information you can return such as the page the user was previously on before landing on this page. <?php echo "Browser: " . $_SERVER['HTTP_USER_AGENT'] . ""; echo "Requested URL: " . $_SERVER['REQUEST_URI'] . ""; echo "Where you come from: " . $_SERVER['HTTP_REFERER'] . ""; echo "IP: " . $_SERVER['REMOTE_ADDR'] . ""; echo "Time of request: " . $_SERVER['REQUEST_TIME'] . ""; echo "Your chosen language: " .…

What does an “&” in front of a variable name mean?

This is a reference to a variables contents, see the example below. <?php $name = 'Chris'; $pointer = &$namer; // Sets $pointer as a reference to $name echo $name; // Prints 'Chris' $pointer = 'Archie'; // Sets $name to 'Archie' echo $name; // Outputs 'Archie' ?> See the PHP manual here

Return the total number of items in an array

// Takes an array and returns the final key (being the last element) function endKey($array){ end($array); return key($array); } The above snippet will take an array and return last items key, so if we have an array with 3 items in it. We'd be returned "2" 0 for the first item 1 for the second 2 for the last item in the array

[Sovled] - mod_fcgid: stderr: PHP Parse error: syntax error, unexpected ‘[’

There's a million ways to get a PHP Parse error, however if you do receive the problem (in my case, working with a plugin on a Expression Engine) which looks similar to PHP Parse error: syntax error, unexpected '[' try tailing your error logs, find out how to tail your logs here. After tailing the logs I noticed the error which looked similar to this mod_fcgid: stderr: PHP Parse error: syntax error, unexpected '[' in /var/www/vhosts/domain.com/httpdocs/system/user/addons/tag/Model/Data.php on…