-
Notifications
You must be signed in to change notification settings - Fork 6
/
ajax.php
29 lines (27 loc) · 1.46 KB
/
ajax.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
<?php
/**
* Created by PhpStorm.
* User: mertcan
* Date: 10.05.2016
* Time: 16:14
*/
session_start();
require('database.php');
if (isset($_POST['url'])) {
$stmt = $db->prepare("SELECT url_short FROM urls WHERE url = :link LIMIT 1");
$stmt->execute(array(':link' => urlencode(htmlspecialchars(trim($_POST['url'])))));
$row = $stmt->fetch(PDO::FETCH_ASSOC);
if ($stmt->rowCount() == 1) {
echo json_encode(array("url" => $server_name . $row["url_short"]));
} else if (empty(trim($_POST["alias"]))) {
//kısaltma algoritması
$short = substr(md5(rand()), 0, 6);
$stmt = $db->prepare("INSERT INTO urls (url, url_short, date, username) VALUES(:url_link,:url_short,:url_data,:username)");
$stmt->execute(array(':url_link' => urlencode(htmlspecialchars(trim($_POST["url"]))), ':url_short' => $short, ':url_data' => date('d.m.Y H:i:s'), ':username' => htmlspecialchars(trim($_SESSION['username']))));
echo json_encode(array("url" => $server_name . $short));
} else {
$stmt = $db->prepare("INSERT INTO urls (url, url_short, date, username) VALUES(:url_link,:url_short,:url_data,:username)");
$stmt->execute(array(':url_link' => urlencode(htmlspecialchars(trim($_POST['url']))), ':url_short' => htmlspecialchars(trim($_POST["alias"])), ':url_data' => date('d.m.Y H:i:s'), ':username' => htmlspecialchars(trim($_SESSION['username']))));
echo json_encode(array("url" => $server_name . $alias_data));
}
}