Code Monkey home page Code Monkey logo

shorty's Introduction

Shorty

Shorty is a simple URL shortener for PHP.

Installation

1. Download and extract the files to your web directory.

2. Use the included database.sql file to create a table to hold your URLs.

3. Configure your webserver.

For Apache, edit your .htaccess file with the following:

RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ index.php?q=$1 [QSA,L]

For Nginx, add the following to your server declaration:

server {
    location / {
        rewrite ^/(.*)$ /index.php?q=$1;
    }
}

4. Edit the config.php file.

Generating short URLs

To generate a short URL, simply pass in a url query parameter to your Shorty installation:

http://example.com/?url=http://www.google.com

This will return a shortened URL such as:

http://example.com/9xq

When a user opens the short URL they will be redirected to the long URL location.

By default, Shorty will generate an HTML response for all saved URLs. You can alter the response format by passing in a format query parameter.

http://example.com/?url=http://www.google.com&format=text

The possible formats are html, xml, text, and json.

Whitelist

By default anyone is allowed to enter a new URL for shortening. To restrict the saving of URLs to certain IP addresses, use the allow function:

$shorty->allow('192.168.0.10');

Requirements

  • PHP 5.1+
  • PDO extension

License

Shorty is licensed under the MIT license.

shorty's People

Contributors

choopm avatar jaywilliams avatar mikecao avatar simpleauthority avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

shorty's Issues

Last PR introduced bug

My last PR introduced a bug that does not allow new URLs to be created because q is empty before that logic is processed. My next PR will fix this behavior.

404 Not Found

I always get this error when open my website

My .htaccess:
RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ index.php?q=$1 [QSA,L]

config.php is finished too

Key length

When doing:
mysql -u root -p new_database < database.sql
I see the error:
ERROR 1071 (42000) at line 4: Specified key was too long; max key length is 767 bytes
using MariaDB.

This seems to be a limitation of the database itself, according to:

https://answers.launchpad.net/maria/+question/241612

Would there be any way to work around this?

Undefined index: q

I followed the guide, added database and htdocs.
But I still get this error.

Notice: Undefined index: q in C:\xampp\htdocs\shorty\shorty.php on line 314
404 Not Found

this is my project path : C:\xampp\htdocs\shorty
this is my htdocs path : C:\xampp\htdocs\shorty.htdocs

this is my config file.

<?php
// Hostname for your URL shortener
$hostname = 'http://localhost.com';
// PDO connection to the database
$connection = new PDO('mysql:dbname=shorty_temp;host=localhost', 'root', '');
$chars = 'abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789';
$salt = '';
$padding = 3;
?>

how can I fix this?

Home page does not show 404

The home page does not show a 404 message because $_GET['url'] is unset at the time of checking. The page is blank, and an error is logged:

2018/04/04 21:51:30 [error] 2990#2990: *21 FastCGI sent in stderr: "PHP message: PHP Notice:  Undefined index: url in /usr/share/nginx/html/v.craftblock.me/shorty.php on line 315
PHP message: PHP Notice:  Undefined index: format in /usr/share/nginx/html/v.craftblock.me/shorty.php on line 316" while reading response header from upstream, client: 172.69.142.45, server: v.craftblock.me, request: "GET / HTTP/1.1", upstream: "fastcgi://unix:/var/run/php/php7.2-fpm.sock:", host: "v.craftblock.me"

404 when going onto shortened URLs

Set everything up following the guide, .htaccess, confs for the domain etc all done, database sorted. When I try to add a URL it generates, but when I click on the URL it doesn't work, instead giving a 404 message.

PHP error

I'm not sure if you maintain this anymore or if you would even be willing to help me with my issue. Regardless I'll ask.

I'm using PHP 7.4 on Ubuntu 20.04.4 using nginx to server my site.
To my knowledge I have configured everything correctly.
Upon testing it does display the shortened url page however it does not finish its execution, therefore the shortened url isn't actually attached to anything.
Checking my error log (/var/log/nginx/error.log) I see it has produced the following exception.

2022/06/27 23:57:33 [error] 739740#739740: *1953 FastCGI sent in stderr: "PHP message: PHP Notice:  Undefined index: q in /var/www/mysite.com/shorty.php on line 314" while reading response header from upstream, client: 172.69.68.17, server: mysite.com, request: "GET /index.php?url=http://www.google.com HTTP/1.1", upstream: "fastcgi://unix:/var/run/php/php7.4-fpm.sock:", host: "mysite.com"

The issue stems from this line

While I do not think this is the cause of my issues it might be relevant.
The mysql user that shorty is using only has INSERT, SELECT, and UPDATE permissions within the scope of shorty.urls

Where are the short URLs stored?

It works fine, I think.

But where are the IDs / the shorted URLs stored?

In the database is only an ID and the long URL but no shorted URL...

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.