php - URL Shortener Issue -
after submitting url via shortener. gives url custom code @ end rather redirecting want give error code:
not found
the requested url /1njchs not found on server.
additionally, 404 not found error encountered while trying use errordocument handle request.
i unsure file may have issue in have linked 3.
shortener.php:
<?php class shortener { protected $db; public function __construct() { //demo purposes $this->db = new mysqli('localhost', 'langers_langers', '^m%kt3&50pwn','langers_website'); } protected function generatecode($num){ return base_convert($num, 10, 36); } public function makecode($url){ $url = trim($url); if(!filter_var($url, filter_validate_url)) { return ''; } $url = $this->db->escape_string($url); //check if url exists $exists = $this->db->query("select code links url ='{$url}'"); if($exists->num_rows){ return $exists->fetch_object()->code; } else { //insert record without code $insert = $this->db->query("insert links (url, created) values ('{$url}', now())"); //generate code based on id $code = $this->generatecode($this->db->insert_id); //update record $this->db->query("update links set code = '{$code}' url = '$url'"); return $code; } } public function geturl($code){ $code = $this->db->escape_string($code); $code = $this->db->query("select url links code = '$code'"); if($code->num_rows){ return $code->fetch_object()->url; } return ''; } } ?>
redirect.php
<?php require_once 'classes/shortener.php'; if(isset($_get['code'])) { $s = new shortener; $code = $_get['code']; if($url = $s->geturl($code)) { header("location: {url}"); die(); } } header('location: index.php');
shorten.php
<?php session_start(); require_once 'classes/shortener.php'; $s = new shortener; if(isset($_post['url'])) { $url = $_post['url']; if($code = $s->makecode($url)) { $_session['feedback'] = "generated! short url is: <a href=\"http://r.langers.co/{$code}\">http://r.langers.co/{$code}</a>"; } else { $_session['feedback'] = "there problem. invalid url, perhaps?"; } } header('location: index.php');
Comments
Post a Comment