This commit is contained in:
2024-09-04 14:05:30 +05:30
parent 55592d3214
commit 8aaea4bd32
5 changed files with 97 additions and 32 deletions

View File

@@ -0,0 +1,24 @@
<?php
session_start();
include "config.php";
// Check if the session variable for storing shortened links exists
if (!isset($_SESSION['shortened_links'])) {
$_SESSION['shortened_links'] = array();
}
// Retrieve and return the list of shortened links for the current session
$links = array();
foreach ($_SESSION['shortened_links'] as $link_id) {
$sql = mysqli_query($conn, "SELECT * FROM url WHERE id = '{$link_id}'");
if (mysqli_num_rows($sql) > 0) {
$row = mysqli_fetch_assoc($sql);
$links[] = array(
'short_url' => $row['shorten_url'],
'full_url' => $row['full_url']
);
}
}
echo json_encode($links);
?>

View File

@@ -1,23 +1,29 @@
<?php
include "config.php";
$full_url = mysqli_real_escape_string($conn, $_POST['full_url']);
if(!empty($full_url) && filter_var($full_url, FILTER_VALIDATE_URL)){
$ran_url = substr(md5(microtime()), rand(0, 26), 5);
$sql = mysqli_query($conn, "SELECT * FROM url WHERE shorten_url = '{$ran_url}'");
if(mysqli_num_rows($sql) > 0){
echo "Something went wrong. Please generate again!";
}else{
$sql2 = mysqli_query($conn, "INSERT INTO url (full_url, shorten_url, clicks)
VALUES ('{$full_url}', '{$ran_url}', '0')");
if($sql2){
$sql3 = mysqli_query($conn, "SELECT shorten_url FROM url WHERE shorten_url = '{$ran_url}'");
if(mysqli_num_rows($sql3) > 0){
$shorten_url = mysqli_fetch_assoc($sql3);
echo $shorten_url['shorten_url'];
}
session_start();
include "config.php";
$full_url = mysqli_real_escape_string($conn, $_POST['full_url']);
if (!empty($full_url) && filter_var($full_url, FILTER_VALIDATE_URL)) {
$ran_url = substr(md5(microtime()), rand(0, 26), 5);
$sql = mysqli_query($conn, "SELECT * FROM url WHERE shorten_url = '{$ran_url}'");
if (mysqli_num_rows($sql) > 0) {
echo "Something went wrong. Please generate again!";
} else {
$sql2 = mysqli_query($conn, "INSERT INTO url (full_url, shorten_url, clicks)
VALUES ('{$full_url}', '{$ran_url}', '0')");
if ($sql2) {
$link_id = mysqli_insert_id($conn); // Get the ID of the newly inserted row
if (!isset($_SESSION['shortened_links'])) {
$_SESSION['shortened_links'] = array();
}
$_SESSION['shortened_links'][] = $link_id; // Store the link ID in session
$shorten_url = $ran_url;
echo $shorten_url;
}
}else{
echo "$full_url - This is not a valid URL!";
}
?>
} else {
echo "$full_url - This is not a valid URL!";
}
?>