Retrieve instance metadata using PHP for IMDSv2

Standard
#!/bin/bash

# Update the system
apt update && apt upgrade -y

# Install Apache and PHP
apt install -y apache2 php libapache2-mod-php

# Enable and start Apache service
systemctl enable apache2
systemctl start apache2

cat <<EOF > /var/www/html/index.php

<?php
$token_url = 'http://169.254.169.254/latest/api/token';
$internal_ip_url = 'http://169.254.169.254/latest/meta-data/local-ipv4';
$public_ip_url = 'http://169.254.169.254/latest/meta-data/public-ipv4';

// Create a stream for token
$opts = [
    "http" => [
        "method" => "PUT",
        "header" => "X-aws-ec2-metadata-token-ttl-seconds: 21600",
    ]
];

// DOCS: https://www.php.net/manual/en/function.stream-context-create.php
$context = stream_context_create($opts);
$token = file_get_contents($token_url, false, $context);

// Create a stream for internal and public IP
$opts_ip = [
    "http" => [
        "method" => "GET",
        "header" => "X-aws-ec2-metadata-token: $token",
    ]
];

$context_ip = stream_context_create($opts_ip);
$internal_ip = file_get_contents($internal_ip_url, false, $context_ip);
$public_ip = file_get_contents($public_ip_url, false, $context_ip);

echo "<h1>EC2 Instance IP Addresses</h1>";
echo "<p>Internal IP: " . $internal_ip . "</p>";
echo "<p>Public IP: " . $public_ip . "</p>";

?>
EOF

# Set proper permissions
chown apache:apache /var/www/html/index.php

Authentication for multiple subdomain in CakePHP

Standard

Let say you have subdomain blog.website.com and help.website.com.

If you login to blog.website.com and when you access help.website.com you notice that you already authenticate althought the subdomain is different.

So how you want to differentiate the session after you login since the default setup of CakePHP will consider you on the same site ?

Edit your bootstrap.php in app / config and try to put this line .

ini_set(‘session.cookie_domain’, env(“HTTP_HOST”));

You can validate your cookie by installing Firebug in Firefox and installing an extension for firebug which is call ‘Firecookie‘.

Cheers ~!

jQuery Multiple File Upload Plugin

Standard

i’m using this plugin http://www.fyneworks.com/jquery/multiple-file-upload/

If you are using it with cakephp , dont just rely on the form helper .. u need to add something to make it work.

<?php echo $this->Form->file(‘attachments’ , array(‘class’ => “multi max-2” , ‘name’ => “data[Attachment][]”)); ?>

notice the [] and also need to overwrite the name because if you dont , it will only upload the latest file that u select even if have already select more than 1 .