Advertisement
Guest User

Untitled

a guest
Jun 3rd, 2018
147
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.08 KB | None | 0 0
  1. <?php
  2. if(isset($_GET['link'])) {
  3.    $test = $_GET['link'];
  4.    echo $test;
  5. } else {
  6.  
  7. }
  8. if ($_SERVER["REQUEST_METHOD"] == "POST") {
  9. $url = $_POST['fname'];
  10. if (filter_var($url, FILTER_VALIDATE_URL)) {
  11.  
  12.  
  13.  
  14.  
  15.  
  16.  
  17.  
  18.  
  19.  
  20. function GeraHash($qtd){
  21. //Under the string $Caracteres you write all the characters you want to be used to randomly generate the code.
  22. $Caracteres = 'abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ1234567890';
  23. $QuantidadeCaracteres = strlen($Caracteres);
  24. $QuantidadeCaracteres--;
  25.  
  26. $Hash=NULL;
  27.    for($x=1;$x<=$qtd;$x++){
  28.        $Posicao = rand(0,$QuantidadeCaracteres);
  29.        $Hash .= substr($Caracteres,$Posicao,1);
  30.    }
  31.  
  32. return $Hash;
  33. }
  34.  
  35. //Here you specify how many characters the returning string must have
  36. $str = GeraHash(6);
  37.  
  38.  
  39.  
  40.  
  41.  
  42.  
  43.  
  44.  
  45.  
  46.  
  47.  
  48.  
  49. $servername = "localhost";
  50. $username = "root";
  51. $password = "";
  52. $dbname = "l";
  53.  
  54. // Create connection
  55. $conn = new mysqli($servername, $username, $password, $dbname);
  56. // Check connection
  57. if ($conn->connect_error) {
  58.    die("Connection failed: " . $conn->connect_error);
  59. }
  60.  
  61. $sql = "SELECT * FROM links WHERE code='$str'";
  62. $result = $conn->query($sql);
  63.  
  64. if ($result->num_rows > 0) {
  65.    // output data of each row
  66.    while($row = $result->fetch_assoc()) { //this is if the record was found
  67.        echo "
  68. <html>
  69. <head>
  70. <title>Link Shortener</title>
  71. </head>
  72. <body>
  73.  
  74.  
  75. <div id='page'>
  76. <span id='top'>Fail!</span><br>
  77. <span id='below'>Please try again, you won't see this error next time!</a>
  78.  
  79. ";
  80.  
  81. //
  82.    }
  83. } elseif ($url == 'https://www.google.com/') { echo "Can't use that link, sorry!";
  84. }else{
  85.  
  86.  
  87. $servername = "localhost";
  88. $username = "root";
  89. $password = "";
  90. $dbname = "l";
  91.  
  92. // Create connection
  93. $conn = mysqli_connect($servername, $username, $password, $dbname);
  94. // Check connection
  95. if (!$conn) {
  96.    die("Connection failed: " . mysqli_connect_error());
  97. }
  98.  
  99. $sql = "INSERT INTO links (code, link, hits)
  100. VALUES ('$str', '$url', '0')";
  101.  
  102. if (mysqli_query($conn, $sql)) {
  103.    echo("
  104.  
  105. <html>
  106. <head>
  107. <title>Link Shortener</title>
  108. </head>
  109. <body>
  110.  
  111.  
  112. <div id='page'>
  113. <span id='top'>Success!</span><br>
  114. <span id='below'><a href='http://localhost/$str'>http://localhost/$str</a>
  115.  
  116.  
  117.      ");
  118. } else {
  119.    echo "Error: " . $sql . "<br>" . mysqli_error($conn);
  120. }
  121.  
  122. mysqli_close($conn);
  123.  
  124. }
  125.  
  126. } else {
  127.    echo("
  128. <html>
  129. <head>
  130. <title>Link Shortener</title>
  131. </head>
  132. <body>
  133.  
  134.  
  135. <div id='page'>
  136. <span id='top'>Fail!</span><br>
  137. <span id='below'>Invalid URL</a>
  138.  
  139. ");
  140. }
  141.  
  142. }else{
  143.  
  144. ?>
  145. <html>
  146. <head>
  147. <title>Link Shortener</title>
  148. </head>
  149. <body>
  150.  
  151.  
  152. <div id="page">
  153. <span id="top">Link Shortener</span><br>
  154. <span id="below">Paste a link below and click shorten to make your long link shorter</span><br><br>
  155.  
  156.  
  157.  
  158. <form method="post">
  159.  <input autocomplete="off" spellcheck="false" type="text" id="fname" name="fname" placeholder="https://www.longlinkhere.com/longlink.12944231#4w23q422" required><br>
  160. <input type="submit" value="Shorten">
  161. </form>
  162. </span>
  163. <?php
  164. }
  165. ?>
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement