PHP file redirect to given link - php

I have search on the web, yet I can't get the answer where a single PHP file could redirect to given url.
Like example I see on the website when I click the contact link on the menu I can see the url like below. And it redirect me to contact page. Even when I try placed other domain on the redirect 'to' location it can work as usual.
What is the best way to have a single file where could work like this
example.com/redir?s=icon_topost&url=http://www.example.com/contact (redir)
or
mydomain.com/redir.php?=http//www.mydomain.com/contact (redir.php)
I have try this code. I can't.
<?php header('Location: http://'); ?>
Thanks.

This code should work, but you must have not returned anything in the body to be able to change headers. What's the error PHP is displaying?
<?php header("Location: $_GET['url']"); ?>

Related

How do I stop user being able to see website code

the dilemma I have is my website index.php calls to a template php file on a button press like this:
case 'main':
$page = getTemplate('main.php', array('user'=>$user));
echo $page;
break;
This main.php template file is in a folder in "/var/www/template/" How do I stop people going to: domain.com/template/main.php and viewing the code for that page. I think the solution would be to make the localhost be able to pull the it and display it rather than the user or something along those lines. Any help would be appreciated thank you.
Like a comment said, the PHP file will not be printed, it will print the HTML result that the php file produce.
Maybe it produces some errors indicating vulnerabilities to a potential attacker ? If that's your case, you should handle this directly into the php code or use a .htaccess at the root of your site. You can't find some help there.
How to deny access to a file in .htaccess
Managed to fix this by putting this at the top of the php page I wanted to render:
<?php
if (!isset($_GET['page'])) {
header('Location: /main');
exit();
}
?>
This means if someone goes "domain.com/template/main.php" to attempt to view the source code, it will redirect them back to the main webpage for my site. Thanks for your suggestions however.

redirect a url to an HTML page

I am trying to route an external webpage to a local file on my machine.
For example:
https://example.org/something/page.php
should redirect to:
c:\local.html
I don't want to redirect all traffic from that domain, only this particular page.
I do not have access to the website so I can't change it.
It's a PHP script. This means you can use PHP's header() function to do what you need.
You'd use it like this:
<?php
header('Location: file://path/to/file.html');
?>
If you want to redirect to a different page on the same server simply use
<?php
header('Location: /path/to/file.php');
?>
This would cause all traffic that lands on that page to be redirected to that local path.
For the best performance you'd want to put this at the top of your script so that the page does not execute more PHP code than needed.
Also do note that any local files will not be visible for other users visiting your page on the interweb.
This means that whenever someone lands on page.php he or she will be redirected to the same path on their local PC so if that is not there they will get a 404 of some kind.
If you want it to happen immediately as you visit the page, add this inside your <head>-tag.
<meta http-equiv="refresh" content="0; url=file:///c:/local.html" />
Same can be achieved with Javascript, if you add this to your <script>-tag inside your <head>-tag.
window.location = "file:///c:/local.html"
And since I see you're using PHP, this might be ideal to you as well:
header('Location: file:///c:/local.html');
Using either one works, but I would prefer the PHP option. Simply add it inside your <php>-tag (at the top would be prefered).

URL Redirection issue for hash(#)

I am sending mail using different file. My Main URL is something like this
http://www.examplehasset.com/enquiry-form.php#quote_page
from this page, i send enquiry. and after sending mail I wrote header loaction to redirect thank you page.
header("Location: enquiry-thanks-you-page.php");
exit;
so this line is working proper to redirect me on thank you page. but one issue is that character after (#) is also appended to my URL. so my thankyou page URL is looks like this.
http://www.examplehasset.com/enquiry-thanks-you-page.php#quote_page
I dont want #quote_page in URL. So let me know what is the method to do so? I just want the URL as follow. http://www.examplehasset.com/enquiry-thanks-you-page.php
Try to redirect with # only [ Work around ] .(See if it works across all browser).
header("Location: enquiry-thanks-you-page.php#"); //See if it works

php redirect() not working on online server

I had redirected my page like:
redirect(base_url().'user/login/?redirect=site/cart_steps/steps');
and it works fine in localhost and when i uploaded it to server it didnt redirect. I checked all the codes and above this all code executes. But when it comes to redirect the page doesnt redirects. I also tried
redirect(base_url().'user/login?redirect=site/cart_steps/steps');
redirect(base_url().'user/login');
header('Location:'.base_url().'user/login?redirect=site/cart_steps/steps');
header('Location:'.base_url().'user/login');
but the page didn't redirect? and also i checked out this The header function is not working on online server?
but it cant help me too.... can any one explain what is the problem here....
You can use the refresh method like this:
<?php
redirect($this->input->server('HTTP_REFERER'), 'refresh'); // refresh the page
?>
Another common mistake could be echoing out / printing something before the redirection.
I would check your CodeIgniter error logs to verify that particular file isn't throwing any error messages that could tell you what needs fixing.
If you haven't already, you'll probably also want to make sure that the URL helper is loaded.
ex:
$this->load->helper('url');
Another solution
<meta http-equiv="refresh" url=http://example.com/">
Try to use like this,
First define your base URL as :
$base_url = "Your base url";
then apply this $base_url variable to your redirect link.
Hope this will help.

need help in particular php job, dont know how to do it

I want to do something but cant figure out how to do this (i m newbie in php)
suppose, i have a list of URL's which shows live with this preg_replace,
$html = preg_replace('/\s(\w+:\/\/)(\S+)/', ' GO ', $html);
my output is like
http://localhost/get.php?url=http://yahoo.com
its obvious that you can view that links at output page, now i want to hide them at front page and make them clickable and working
something like we can change links into variables and then call them by clicking and something works in backgroud which can perform same thing as we are clicking on the link at front page
ya it seems bit confusing :(
you could save the url into a $_SESSION vars and when some users click the link retrive the url from $_SESSION and redirect to it...
//page1 - parse, save link in session and print a call to page2
<?php
session_start();
$_SESSION['url'] = preg_replace('/\s(\w+:\/\/)(\S+)/', ' GO ', $html);
....
echo 'GO';
?>
//page2
<?php
session_start();
header('Location: '.$_SESSION['url']);
?>
If i undeerstood what you meant...
Obviously now I used $_SESSION['url'] as a single string, but you can use a multidimensional array intead...
UPDATE:
anyway is better if you use an array on script..
example: http://www.test.org/go.php?page=# (where # is a number)
<?php
$array=("http://www.google.com","http://stackoverfloc.com","ecc");
//you can add more contorl in if statement, like between etc...
if (is_numeric($_GET['page']) header('Location: '.$array[$_GET['page']]);
?>
I'm sorry, maybe I'm not understanding quite well, but, isn't best approch use an array for in side server and use another Get variable to do that?
for example ?link=yahoo
and then
find link in array of url?
BY the way, I'm using NoScript and reports me like a warning..
You build up links with looking like this:
yourdomain.com/redirector.ph?url=#
where # represents an identifier.
In redirector.php you check if you know that identifier and send the redirect HTTP Header:
header("Location: http://www.example.com/");
Important Note:
You may not send any data before sending the header and the code after sending it won't be executed.
Info
http://php.net/manual/en/function.header.php
EDIT
Using this header is not absolutely proper in regards to standard, but it's not too far off, as the response really is at another location.

Categories