I've made a shorten url website. But the thing is not working that i can't include a file. It works and redirects if i go directly using url [closed]

StackOverflow https://stackoverflow.com/questions/19280333

Question

I've made a shorten url website. But the thing is not working. I can't include a file. It works and redirects if I go directly using a url.

my handler page code is

<?php

plink = explode('/',$_GET['p']);
//print_r($plink);

include ($_SERVER['DOCUMENT_ROOT'].'/core/config/file.php');

$code = $_GET['p'];

$query = mysql_query("SELECT * FROM shortenurl WHERE code = '$code'");
$numrows = mysql_num_rows($query);

if($numrows == 1){
    $row = mysql_fetch_assoc($query);
    $url = $row['url'];

//  header ("Location: $url");  
}

//starts url masking
if (isset($_GET['p']))
{
    switch($_GET['p'])
    {

// pages set for common interface       
       case 'home':
       include "pages/home.php";
       break;

       case $code:
       include ($_SERVER['DOCUMENT_ROOT'].'/core/pages/redirect.php?r=$url');
       break;      

//defualt page 404 Error page is set       
       default;
       include ($_SERVER['DOCUMENT_ROOT'].'/errorpages/404.php');
       break;
    }
}?>

I have added google in database and shorten url in localhost is "site.com/AasfAS"

but I'm getting the following error

Warning: include(C:/xampp/htdocs/core/pages/strg_rd.php?r=googlesite): failed to open stream: No such file or directory in C:\xampp\htdocs\core\pagehandler.php on line

Warning: include(): Failed opening 'C:/xampp/htdocs/core/pages/strg_rd.php?r=googlesite' for inclusion (include_path='.;C:\xampp\php\PEAR') in C:\xampp\htdocs\core\pagehandler.php on line "

but if I go to the url /core/pages/strg_rd.php?r=googlesite
it redirects me to the google website
How do I solve this?

Was it helpful?

Solution

Change your code to this.

case $code:
       $_GET['r'] = $url;
       include ($_SERVER['DOCUMENT_ROOT'].'/core/pages/redirect.php');
       break; 

OTHER TIPS

include just puts in the contents of a file into the file you're working on. There is a file called redirect.php (I assume), but there's no file called redirect.php?r=.... What you probably want to do is to simply do the redirect in the same file directly.

Since this is PHP, all you should need is to replace:

include ($_SERVER['DOCUMENT_ROOT'].'/core/pages/redirect.php?r=$url');

with

header( 'Location: $url' ) ;

This code:

$code = $_GET['p'];

$query = mysql_query("SELECT * FROM shortenurl WHERE code = '$code'");
$numrows = mysql_num_rows($query);

Is a disaster waiting to happen.
Fix the sql-injection hole before your site gets pwned like so:

$code = mysql_real_escape_string($_GET['p']);

$query = mysql_query("SELECT * FROM shortenurl WHERE code = '$code'");
$numrows = mysql_num_rows($query);

More info is here: How can I prevent SQL injection in PHP?

Or even better drop the mysql_ lib (It was depreciated 10 years ago!) and use PDO.

See here for more info: http://php.net/manual/en/book.pdo.php

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top