I am using this code to create a urlshortner, that if a user from US visit the link he will be redirected to some page and if a user from any other country visit the link he will be redirected to some other page. It is not giving me any errors but not working either.
<?php
$con = mysqli_connect("localhost","root","");
if (!$con){
die('Could not connect: ' . mysql_error());
}
mysqli_select_db($con, "shortenedurls"); //Replace with your MySQL DB Name
$de= mysqli_real_escape_string($con, $_GET["decode"]);
$sql = 'select * from short_urls where shorturl="$de"';
$urlid = mysqli_query($con,'SELECT id FROM short_urls WHERE shorturl="$de"');
$result=mysqli_query($con, 'SELECT * FROM redirect WHERE urlid="$urlid" LIKE "%US%"') or die(mysqli_error($con));
if(mysqli_num_rows($result) == 0 ) {
$result1 = mysqli_query($con, 'SELECT * FROM short_urls WHERE shorturl="$de"');
while($row = mysqli_fetch_array( $result1)) {
$res=$row['urlinput'];
header("location:".$res);
}
} else {
while($row = mysqli_fetch_array( $result)) {
$res=$row['urlinput1'];
header("location:".$res);
}
}
?>
"$de"
will not resolve correctly as it's also within single quotes; and they take precedence. Swap single and double quotes and that issue is fixed.
For example
$urlid = mysqli_query($con,"SELECT id FROM short_urls
WHERE shorturl='$de'");
Also this query is syntactically incorrect
$result=mysqli_query($con, "SELECT * FROM redirect
WHERE urlid='$urlid' LIKE '%US%'")
Should be
$result=mysqli_query($con, "SELECT * FROM redirect
WHERE urlid IKE '%US%'")
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.