Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- <?php
- if(isset($_GET['link'])) {
- $test = $_GET['link'];
- echo $test;
- } else {
- }
- if ($_SERVER["REQUEST_METHOD"] == "POST") {
- $url = $_POST['fname'];
- if (filter_var($url, FILTER_VALIDATE_URL)) {
- function GeraHash($qtd){
- //Under the string $Caracteres you write all the characters you want to be used to randomly generate the code.
- $Caracteres = 'abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ1234567890';
- $QuantidadeCaracteres = strlen($Caracteres);
- $QuantidadeCaracteres--;
- $Hash=NULL;
- for($x=1;$x<=$qtd;$x++){
- $Posicao = rand(0,$QuantidadeCaracteres);
- $Hash .= substr($Caracteres,$Posicao,1);
- }
- return $Hash;
- }
- //Here you specify how many characters the returning string must have
- $str = GeraHash(6);
- $servername = "localhost";
- $username = "root";
- $password = "";
- $dbname = "l";
- // Create connection
- $conn = new mysqli($servername, $username, $password, $dbname);
- // Check connection
- if ($conn->connect_error) {
- die("Connection failed: " . $conn->connect_error);
- }
- $sql = "SELECT * FROM links WHERE code='$str'";
- $result = $conn->query($sql);
- if ($result->num_rows > 0) {
- // output data of each row
- while($row = $result->fetch_assoc()) { //this is if the record was found
- echo "
- <html>
- <head>
- <title>Link Shortener</title>
- </head>
- <body>
- <div id='page'>
- <span id='top'>Fail!</span><br>
- <span id='below'>Please try again, you won't see this error next time!</a>
- ";
- //
- }
- } elseif ($url == 'https://www.google.com/') { echo "Can't use that link, sorry!";
- }else{
- $servername = "localhost";
- $username = "root";
- $password = "";
- $dbname = "l";
- // Create connection
- $conn = mysqli_connect($servername, $username, $password, $dbname);
- // Check connection
- if (!$conn) {
- die("Connection failed: " . mysqli_connect_error());
- }
- $sql = "INSERT INTO links (code, link, hits)
- VALUES ('$str', '$url', '0')";
- if (mysqli_query($conn, $sql)) {
- echo("
- <html>
- <head>
- <title>Link Shortener</title>
- </head>
- <body>
- <div id='page'>
- <span id='top'>Success!</span><br>
- <span id='below'><a href='http://localhost/$str'>http://localhost/$str</a>
- ");
- } else {
- echo "Error: " . $sql . "<br>" . mysqli_error($conn);
- }
- mysqli_close($conn);
- }
- } else {
- echo("
- <html>
- <head>
- <title>Link Shortener</title>
- </head>
- <body>
- <div id='page'>
- <span id='top'>Fail!</span><br>
- <span id='below'>Invalid URL</a>
- ");
- }
- }else{
- ?>
- <html>
- <head>
- <title>Link Shortener</title>
- </head>
- <body>
- <div id="page">
- <span id="top">Link Shortener</span><br>
- <span id="below">Paste a link below and click shorten to make your long link shorter</span><br><br>
- <form method="post">
- <input autocomplete="off" spellcheck="false" type="text" id="fname" name="fname" placeholder="https://www.longlinkhere.com/longlink.12944231#4w23q422" required><br>
- <input type="submit" value="Shorten">
- </form>
- </span>
- <?php
- }
- ?>
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement