I’m not thrilled that I have to create this cheatsheet, but such is life.

Embed HTML in php

<?php
echo "<p>Stuff</p>";
?>

Resource: https://stackoverflow.com/questions/18140270/how-to-write-html-code-inside-php

Setup Debugger with PHP w/ PHPStorm and XAMPP

Start out by installing phpstorm and xampp.

Follow the process in here: https://www.techflirt.com/install-configure-xdebug-on-xampp-windows-and-mac/

For the above process, don’t forget to install xdebug for the version of php in xampp, and not the OS version:

/Applications/XAMPP/bin/php -v

Make sure you do everything in here: https://confluence.jetbrains.com/display/PhpStorm/Xdebug+Installation+Guide

Be sure to validate that the debugger configuration is set up properly: https://confluence.jetbrains.com/display/PhpStorm/Validating+Your+Debugging+Configuration

If you need to test with requests, you can do so by doing the following:

  1. Go to Run
  2. Edit Configurations
  3. Click +
  4. Select PHP HTTP Request
  5. Select your XAMPP server for the Server:
  6. Specify the URL for the page you want to test
  7. If you need to do a POST, specify the request method and put the post information in the Request body.
  8. Click OK

Nginx and PHP on Ubuntu

This is for testing - do not use this to run a production environment.

# Install and start nginx
sudo apt update && apt install -y nginx
/etc/init.d/nginx start

# Install and start PHP-FPM
sudo apt install php7.2 php7.2-fpm php7.2-cli php7.2-curl php7.2-mysql php7.2-curl php7.2-gd php7.2-mbstring php-pear -y
/etc/init.d/php7.2-fpm start

Configure Nginx:

Replace the contents of /etc/nginx/nginx.conf with this:

user www-data;
worker_processes auto;
pid /run/nginx.pid;
include /etc/nginx/modules-enabled/*.conf;

events {
 worker_connections 768;
 # multi_accept on;
}

http {

 #
 # Basic Settings
 #

 sendfile on;
 tcp_nopush on;
 tcp_nodelay on;
 keepalive_timeout 65;
 types_hash_max_size 2048;
 server_tokens off;

 # server_names_hash_bucket_size 64;
 # server_name_in_redirect off;

 include /etc/nginx/mime.types;
 default_type application/octet-stream;

 #
 # SSL Settings
 #

 ssl_protocols TLSv1 TLSv1.1 TLSv1.2; # Dropping SSLv3, ref: POODLE
 ssl_prefer_server_ciphers on;

 #
 # Logging Settings
 #

 access_log /var/log/nginx/access.log;
 error_log /var/log/nginx/error.log;

 #
 # Gzip Settings
 #

 gzip on;

 # gzip_vary on;
 # gzip_proxied any;
 # gzip_comp_level 6;
 # gzip_buffers 16 8k;
 # gzip_http_version 1.1;
 # gzip_types text/plain text/css application/json application/javascript text/xml application/xml application/xml+rss text/javascript;

 #
 # Virtual Host Configs
 #

 include /etc/nginx/conf.d/*.conf;
 include /etc/nginx/sites-enabled/*;
}

Replace the contents of /etc/nginx/sites-available/default with this:

server {
  listen 80 default_server;
  listen [::]:80 default_server;

  root /var/www/html;

  index index.html index.htm index.nginx-debian.html;

  server_name localhost;

  location / {
    try_files $uri $uri/ =404;
  }

  location ~ \.php$ {
    try_files $uri =404;
    fastcgi_split_path_info ^(.+\.php)(/.+)$;
    fastcgi_pass localhost:8080;
    fastcgi_index index.php;
    fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
    include fastcgi_params;
  }
}

Be sure to change the server_name to your actual server name

Configure PHP-FPM:

sed -i 's/;cgi.fix_pathinfo.*/cgi.fix_pathinfo=0/g' /etc/php/7.2/fpm/php.ini

Fix 502 issue

Run this to fix a 502 issue when you try to access php files:

sed -i 's/listen = \/run\/php\/php.*.sock/listen = localhost:8080/' /etc/php/7.2/fpm/pool.d/www.conf

Restart services

/etc/init.d/nginx restart
/etc/init.d/php7.2-fpm restart

At this point, you should be able to add php files to /var/www/html.

This will create a test file:

echo "<?php phpinfo(); ?>" > /var/www/html/info.php

Resources:


Fix cannot modify header information

I encountered this when using setcookie. The solution is to put this at the top of the file:

<?php ob_start(); ?>

This will effectively stop any output from being sent until server-side processing has completed. Subsequently, this can ensure that expected rendering happens in the proper order.

Resource: https://www.webdeveloper.com/d/74202-resolved-quotcannot-modify-header-informationquot