首页 > 解决方案 > 数据库中的 url 不完整

问题描述

我正在尝试制作一个 url 缩短器,我解决了很多问题,但此时还有两个问题,例如 domain.com/XXX 重定向到 amazon.com/abcéxyz 它将我重定向到 amazon.com/abc 所以它基本上被削减了在第一个重音字母处关闭?我的数据库是一个mysql本地数据库这里是

另一个问题是正确重定向到一个链接,因为当我在这里重定向到 dofus.com 它在 Phpstorm 中给了我“CORS”错误,所以我需要知道如何避免这种情况谢谢

<?php
$pdo = '';
require_once 'config.php';

function crypto_rand_secure($min, $max)
{
    $range = $max - $min;
    if ($range < 1) return $min; // not so random...
    $log = ceil(log($range, 2));
    $bytes = (int) ($log / 8) + 1; // length in bytes
    $bits = (int) $log + 1; // length in bits
    $filter = (int) (1 << $bits) - 1; // set all lower bits to 1
    do {
        $rnd = hexdec(bin2hex(openssl_random_pseudo_bytes($bytes)));
        $rnd = $rnd & $filter; // discard irrelevant bits
    } while ($rnd > $range);
    return $min + $rnd;
}

function getToken($length)
{
    $token = "";
    $codeAlphabet = "ABCDEFGHIJKLMNOPQRSTUVWXYZ";
    $codeAlphabet.= "abcdefghijklmnopqrstuvwxyz";
    $codeAlphabet.= "0123456789";
    $max = strlen($codeAlphabet); // edited

    for ($i=0; $i < $length; $i++) {
        $token .= $codeAlphabet[crypto_rand_secure(0, $max-1)];
    }

    return $token;
}

if(isset($_GET['id'])) {
    $stmt = $pdo->prepare("SELECT * FROM url WHERE shortenurl=:id");
    $stmt->execute(['id' => $_GET["id"]]);
    $data = $stmt->fetch(PDO::FETCH_ASSOC);
    header('Location:' .$data["primaryurl"], true, 302);
    exit();
}

?>

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="utf-8">
    <title>Remy Barranco</title>
    <meta name="description" content="URL Shortener">
    <meta name="author" content="Rémy Barranco">
    <link rel="stylesheet" href="css/styles.css">
</head>

<body>
<div id="form">
<form method="post" action="<?=$_SERVER['PHP_SELF'];?>">
    <input type="text" name="primaryurl" placeholder="Paste here your url" id="primaryurl" autocomplete="off" required/>
    <input type="submit" value="Short"/>
</form>

</div>
<div id="shortened">
    <?php
    if($_SERVER["REQUEST_METHOD"] == "POST") {
        $shortened = getToken(6);
        $url = [
            'primaryurl' => $_POST['primaryurl'],
            'shorturl' => $shortened

        ];
        $sql = "INSERT INTO url (primaryurl, shortenurl) VALUES (:primaryurl, :shorturl)";
        $pdo->prepare($sql)->execute($url);
        unset($pdo);
        echo "your shorten url is ", $shortened;
    }
    ?>
</div>
</body>
</html>

标签: phpmysqlurl

解决方案


我发现了这个,它工作得很好

function redirect($url){
    echo '<script type="text/javascript">';
    echo 'window.location.href="'.$url.'";';
    echo '</script>';
    echo '<noscript>';
    echo '<meta http-equiv="refresh" content="0;url='.$url.'" />';
    echo '</noscript>'; exit;}

推荐阅读