zsh.li Pinkhat Memories Me About ?

Dear My
Linux

Very simple link shortener in PHP

POST_AT

I just simple had this idea, I'm not a programmer, so this is probably not the best solution, before proceeding think of it as something I did because I was bored

I had this idea, saving all the links in a text file each one per line, then converting the number line to another numeric system similar to HEX which also uses letters, the alphanumeric filesystem that is indeed the numeric system with base 36.

I like using both Apache and Nginx, in this case I will show you the Apache directive you will need, AI helped me a lot for writing the code, most of the code was written by AI, by instance part of the code was written using this input:

write php code that takes the URL, which will be provided in alphanumeric format, for example .com/A, where A is the alphanumeric number, which in the decimal system is 10. Once the portion of the URL in alphanumeric format is extracted, it must be transformed into decimal format. with this variable the line number in the links.txt file will be queried, which contains a URL per line. once the line number is queried, it will redirect to the URL found in this line number.

Originally I didn't pretended to create a user form to submit the url via the web but only modifying the file with SSH and the command line, which you can still do so only using the part of the code in the else statement. However you would need a bash script or something for editing the file in the console. Or probably, just using decimal numeration and an editor with number line activated. I was thinking in a VIM plugin that uses the alphanumeric numeric system for number line.

Add the following directive in your Apache configuration, replace the PATH with the yours:

<Directory "/var/www/7d.gg"\> RewriteEngine on RewriteCond %{REQUEST_FILENAME} !-f RewriteCond %{REQUEST_FILENAME} !-d RewriteRule ^(.*)$ /index.php [L] </Directory>

Now create index.php with the following content:

<?php $url = $_SERVER['REQUEST_URI']; if ($url == '/') { include 'main.php'; } else { // Getting alphanumeric string / $alfanumerico = substr($url, strrpos($url, '/') + 1); // Convert alphanumeric string to decimal $decimal = base_convert($alfanumerico, 36, 10); // Open the links.txt file $archivo = fopen("links.txt", "r"); // Counting lines until getting the one $linea = 1; while (!feof($archivo) && $linea < $decimal) { fgets($archivo); $linea++; } // Getting number line $url_destino = fgets($archivo); // Closing file fclose($archivo); // Redirecting header("Location: $url_destino"); } >?

It's a good idea to change permissions of links.txt to 750. I believe this code is not very efficient with a large database.

With this html code you may create a simple form in main.php: <form action="" method="post"> <label for="texto">Escribe el texto que quieres añadir al final de links.txt:</label> <input type="text" id="texto" name="texto" required> <button type="submit">Enviar</button> </form> And this would be the php code: <?php // Verificar si se recibió el texto del usuario if (isset($_POST['texto'])) { // Obtener el texto del usuario $texto = $_POST['texto']; // Abrir el archivo links.txt en modo append $archivo = fopen("links.txt", "a"); // Escribir el texto del usuario en el archivo, con un salto de línea al final fwrite($archivo, $texto . "\n"); // Mostrar un mensaje de confirmación fclose($archivo); // Abrir el archivo links.txt en modo lectura $archivo = fopen("links.txt", "r"); // Inicializar el contador de líneas $contador = 0; // Recorrer el archivo línea por línea while (!feof($archivo)) { // Leer una línea del archivo fgets($archivo); $contador++; } $contador--; // Cerrar el archivo fclose($archivo); // Convertir el contador a la base 36 $alfanumerico = base_convert($contador, 10, 36); // Mostrar el resultado echo "Your link has been added, your link is 7d.gg/" . $alfanumerico; } ?>