0

I have XAMPP and I want to write a simple PHP page, that redirects me to the link that I specify, and also saves the link in an SQL database.

Let's say I want to visit www.google.com:

I'd visit something like:

localhost:80/redirect.php?url=https://google.com 

And PHP would redirect me there and also save the www.google.com link in an SQL table.

Can you help me out?

Nick
  • 331
  • 3
  • 14

2 Answers2

4

Considering how you formed your question, it looks as if you had an idea an just want someone to give you the solution without you even making an effort (please correct me if I'm wrong but that's how it seams...)

The task you are trying to achieve is a simple one, and it's only fair to point you in the right direction. your "task" can be broken into several smaller ones:

  1. Create database / table for storing data | PHP Create MySQL Tables
  2. Get URL parameter in PHP
  3. PHP Insert Data Into MySQL
  4. How to make a redirect in PHP

Sorry if this is not the kind-a answer you are looking for, but I figure the point of this website is for people to learn something and not just copy+paste. The provided links can be used to solve your task problem.

Community
  • 1
  • 1
  • I'm stuck here: $sql = "INSERT INTO log (link, last_visit_date, visit_count) VALUES ($link , $datetime , 1) ON DUPLICATE KEY UPDATE last_visit_date = $datetime, visit_count = visit_count + 1 "; but is of TEXT/VARCHAR(256) and can't be PRIMARY KEY. – Nick Nov 23 '15 at 21:24
  • I'm also stuck here: $sql = "REPLACE INTO log (link, last_visit_date, visit_count) SELECT ($link , $datetime , 1) ON DUPLICATE KEY UPDATE last_visit_date = $datetime, visit_count = visit_count + 1 "; because is of TEXT/VARCHAR(256) and can't be PRIMARY KEY – Nick Nov 23 '15 at 21:24
  • and finally, I'm stuck here: $sql = "IF EXISTS (SELECT * FROM logging WHERE link=$link) THEN UPDATE log SET last_visit_date = $datetime, visit_count = visit_count + 1 WHERE link=$link ELSE INSERT INTO log (link, last_visit_date, visit_count) VALUES ($link , $datetime , 1) END IF"; – Nick Nov 23 '15 at 21:24
0

This is what I came up with, after MySQLi Object-oriented did not validate this:

$sql = "SELECT * FROM logging WHERE link=$link";
if ($conn->query($sql) === TRUE) {}

It still increments the number of visits sometimes by +2. I don't know why.

  <?php
    $servername = " ";
    $username = " ";
    $password = " ";
    $dbname = " ";

    $datetime = date_create()->format('Y-m-d H:i:s');
    $datetime = "'".$datetime."'";

    $link_clean = $_GET['link'];    
    $link = "'".$link_clean."'";


    // Create connection
    $conn = mysqli_connect($servername, $username, $password, $dbname);
    // Check connection
    if (!$conn) {
        die("Connection failed: " . mysqli_connect_error());
    }

    $sql = "SELECT * FROM logging WHERE link=$link";

    if ($result = mysqli_query($conn, $sql)) 
    {

        if(mysqli_num_rows($result)>0)
        {
            $sql="UPDATE logging SET last_visit_date = $datetime, visit_count = visit_count + 1 WHERE link=$link";
            if (mysqli_query($conn, $sql))  {
                $conn->close();
                header("Location: https://$link_clean");
                exit;
            } else {
                echo "1Error: " . $sql . "<br>" . mysqli_error($conn);
                $conn->close();
                exit;
            }
        }
        else
        {
            $sql="INSERT INTO logging (link, last_visit_date, visit_count) VALUES ($link , $datetime , 1)";
            if (mysqli_query($conn, $sql)) {
                mysqli_close($conn);
                header("Location: https://$link_clean");
                exit;
            } else {
                echo "2Error: " . $sql . "<br>" . mysqli_error($conn);
                mysqli_close($conn);
                exit;
            }
        }
    } 

    else 
    {
        echo "3Error: " . $sql . "<br>" . mysqli_error($conn);
    }

    mysqli_close($conn);
    ?>
Nick
  • 331
  • 3
  • 14