Home > Back-end >  Php redirct each data to each url
Php redirct each data to each url

Time:01-09

I have a database where I am storing data. Here every full link has a offer link. What I am trying to do? I want to redirect every single data to every different URL. But I failed.

Example:1

full link: http://localhost/LearnPHP/test/short2.php/redir&q=https://google.com

redirect link: google.com

Example 2

full link: http://localhost/LearnPHP/test/short2.php/redir&q=https://youtube.com

redirect link: youtube.com

Someone when visiting the full link it should redirect each offer link

Database

database

Data Insert

PHP Data insert

PHP Code PHP

CodePudding user response:

You should really post code instead of images. You can post code blocks by posting your code between 3 backticks (`) at the top, and three backticks at the bottom.

However, I tried to copy the code, and edited it. I don’t understand exactly what you are trying to do, but I just made some changes.

Data Insert

<?php 

include 'db.php’;

$first = "http://localhost/LearnPHP/test/short2.php/redir&q=";
$magic = urlencode($_POST["longUrl"]);

if($magic) {
    $finalUrl = $first . $magic;
    $stmt = $conn->prepare("INSERT INTO url (offer_link, full_link) VALUES (?, ?)");
    $stmt->bind_param("ss", $magic, $finalUrl);
    $stmt->execute();
} else {
    $finalUrl = '';
}

?>

In the above code, you can obviously tell I changed some things. First, I don’t have a checking statement inside the insert file. I have put it directly inside of the db.php file. I will post the code for it.

Next, I patched the vulnerability of mySQL injection (https://portswigger.net/web-security/sql-injection) using prepared statements.

db.php

<?php

$servername = "localhost";
$username = "your_username";
$password = "your_password";
$dbname = "your_database_name";

// Create connection
$conn = mysqli_connect($servername, $username, $password, $dbname);

// Check connection
if (!$conn) {
    die("Connection failed: " . mysqli_connect_error());
}

?>

For the last bit, can you please tell me exactly what you need to do? Your post isn’t that helpful. Do you want to use $_GET[‘’] for the redirection? Please edit your code. Read this as well: https://stackoverflow.com/help/how-to-ask

CodePudding user response:

For redirection part you can do something like this:

if(isset($_GET['q'])) // if q param available
{
    $redirectUrl = urldecode($_GET['q']); // get link
    header("Location: $redirectUrl"); // redirect
}

I think that your problem is related to URL decode / encode: LINK

  • Related