Skip to content

Instantly share code, notes, and snippets.

View danielhaim1's full-sized avatar

Daniel Haim danielhaim1

View GitHub Profile
@danielhaim1
danielhaim1 / get_client_ip.php
Last active March 19, 2023 22:19
The get_client_ip() function has been updated to use a whitelist approach to validate the IP address. It checks if the IP address is in one of the trusted ranges specified in an array, and returns the IP address if it is. The function also includes a get_ip_address() function that returns the IP address by checking several server variables. A se…
function get_client_ip() {
$ipaddress = 'UNKNOWN';
$trusted_ranges = [
'192.0.2.0/24', // Example trusted range
// Add more trusted ranges here
];
foreach ($trusted_ranges as $range) {
if (ip_in_range(get_ip_address(), $range)) {
$ipaddress = get_ip_address();
break;
@danielhaim1
danielhaim1 / resizeIframe.js
Last active March 19, 2023 22:20
The resizeIframe() function has been updated to use ES6 syntax and includes optimization with requestAnimationFrame() to reduce layout thrashing.
const buffer = 20; // scroll bar buffer
const iframe = document.getElementById('ifm');
const pageY = elem => elem.offsetParent ? (elem.offsetTop + pageY(elem.offsetParent)) : elem.offsetTop;
const resizeIframe = () => {
window.requestAnimationFrame(() => {
const height = document.documentElement.clientHeight;
const iframeTop = pageY(document.getElementById('ifm'));
const heightDiff = height - iframeTop + buffer;
@danielhaim1
danielhaim1 / color-harmony.scss
Last active March 16, 2017 16:17
Establish color harmony by finding high value, and cool color
//Pick a color
$first-color: hsl(10, 100%, 50%);
// Find the complement
$second-color: complement($first-color);
//Check if you have a cool color on your hands. Cool colors will overpower warm colors when mixing.
@danielhaim1
danielhaim1 / get_share_link.php
Last active March 19, 2023 22:22
A function that curates share-permalinks to Facebook, Twitter, Reddit, Tumblr, Google+, Pinterest, Stumbleupon, etc. The function generates the URL for you.
@danielhaim1
danielhaim1 / randomlyShowElements.js
Last active March 19, 2023 22:24
Fisher-Yates shuffle algorithm to randomly select elements to show/hide.
window.addEventListener('DOMContentLoaded', function() {
var elements = document.querySelectorAll('.changing');
var numOfElements = elements.length;
var numOfElementsToShow = 5;
if (numOfElements <= numOfElementsToShow) {
for (var i = 0; i < numOfElements; i++) {
elements[i].style.display = 'block';
}
return;
@danielhaim1
danielhaim1 / disableinput.js
Last active February 24, 2017 03:17
Disable input fields
$('input[type="submit"]').attr("disabled", true);
@danielhaim1
danielhaim1 / hoverclass.js
Created January 28, 2014 23:04
Toggle class on :hover
$('.class').hover(function(){
$(this).addClass('hover');
}, function(){
$(this).removeClass('hover');
});
@danielhaim1
danielhaim1 / brokenimgfix.js
Last active March 19, 2023 22:12
The JavaScript function replaces broken images with "broken.png" when an image fails to load.
document.querySelectorAll('img').forEach(img => {
img.addEventListener('error', () => {
img.src = 'img/broken.png';
});
});
@danielhaim1
danielhaim1 / swap.js
Last active March 19, 2023 22:14
The JavaScript function adds an event listener to each element, finds another element based on the ID, and swaps its content with the original.
const highlightsBox = document.querySelectorAll('.highlights-box')[0];
const highlightsLinks = document.querySelectorAll('.highlights-link[id^="s"]');
if (highlightsBox && highlightsLinks) {
highlightsLinks.forEach(link => {
link.addEventListener('click', event => {
event.preventDefault();
const id = link.getAttribute('id');
const srcBox = document.querySelectorAll(`.swap-box.${id}`)[0];
@danielhaim1
danielhaim1 / setEqualHeight.js
Last active March 19, 2023 22:17
The function sets the height of all elements in a given selector to the height of the tallest element. It's optimized to avoid redundancy.
function debounce(func, wait) {
let timeout;
return function() {
const context = this, args = arguments;
clearTimeout(timeout);
timeout = setTimeout(() => {
func.apply(context, args);
}, wait);
};
}