# Simple url shortener backend
This is only intended for personal use, possibly giving urls to a few friends.
Nothing that requires scaling.
This is made by Gemini using the following prompts.
The frontend then sends JSON to the backend of the form
```json
{
"short": "never",
"url": "https://gonna.give.you.up/"
}
```
To delete, set the url to `!delete`.
Then one can write a simple frontend with Javascript.
CORS headers are sent in the `cors.php` file if needed (e.g. if the add short interface is on a different domain than the get short frontend, nice for security as the main user-facing frontend domain has no capacity to add or delete).
```php
url", "short": short, "url": url }, or { "message": "deleted abc" } or { "error": "error message" . So a success is indicated by the presence of a "message" element, or else there is an "error" element. If there is a PHP error of some sort, the client should write this to the console.
The top of the PHP backend has an easy way to enable/disable detailed error printing in the case of PHP errors.
The backend database is Mysql.
```
# 2 Prompt for Gemini
```g
The dbauth.php defines
define('HOST',"...");
define('PORT',...);
define('USER',"...");
define('PASS',"...");
define('DB',"...");
```
## Example reporting errors (from Gemini)
```js
fetch('api/index.php', {
method: 'POST',
headers: { 'Content-Type': 'application/json' },
body: JSON.stringify({ short: 'nas', url: 'http://nas-server/storage' })
})
.then(response => {
if (!response.ok) {
// This catches 500 errors and logs the PHP output to console
response.text().then(text => console.error("PHP Error:", text));
}
return response.json();
})
.then(data => {
if (data.message) console.log("Success:", data.message);
if (data.error) console.warn("App Error:", data.error);
});
```
# table create (from Gemini)
```g
CREATE TABLE IF NOT EXISTS `YOURPREFIX_short_to_url` (
`short` VARCHAR(191) NOT NULL,
`url` TEXT NOT NULL,
PRIMARY KEY (`short`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_unicode_ci;
```
# table create php (from Gemini)
```php
connect_error) {
throw new Exception("Connection failed: " . $mysqli->connect_error);
}
// 2. Prepare the Table Name
$tableName = PREFIX . "_short_to_url";
// 3. Define the SQL
// We use VARCHAR(191) for 'short' to ensure compatibility with utf8mb4 indexing
$sql = "CREATE TABLE IF NOT EXISTS `$tableName` (
`short` VARCHAR(191) NOT NULL,
`url` TEXT NOT NULL,
PRIMARY KEY (`short`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_unicode_ci;";
// 4. Execute
if ($mysqli->query($sql) === TRUE) {
echo "