I need to redirect visiters from:
/plug/survey/survey.php?22
to
/publications.php?1.articles.view.547
I have a limited understanding of .htaccess and php and wonder if anyone has any tips/ideas for me?
Any help would be very much appreciated!
Thank you
Add to the top of survey.php:
<?php
if ($_SERVER['QUERY_STRING'] == "22") {
header("Location: http://example.com/publications.php?1.articles.view.547");
exit;
}
You can write this code in htaccess file
RewriteEngine on
Redirect /plug/survey/survey.php?22 /publications.php?1.articles.view.547
Also read this
http://corz.org/serv/tricks/htaccess2.php
if it is just the one file, you can use header('Location: '.$url); at the top of the php - see http://php.net/manual/en/function.header.php
Your question really lacks vital information:
Where does that "1.articles" come from - is it a fixed string ?
where dies that "547" come from, I guess it is from a database lookup somehow ?
If so, there is no easy way to do that using plain rewrite rules.
Most likely the best solution is to write a small php script you redirect to. Inside that script you evaluate the request parameters (php variables $_SERVER and so on), make you database lookup and use the information gathered to send a redirect header to the browser (using phps 'header()' method).
I think your solution is
header("location:/publications.php?1.articles.view.547");
You can use .httaccess, but if you want the user should go on that page you cant use it because it will redirect you before reading any code on that page, but header() will first read the code and if any code is something not good then redirect like this,
if($varisgood){
// not redirect
}
else{
//redirect
}
Related
I want to handle when user input "examp" it will redirect to "examp.php".
I think it's pretty simple but I haven't been able to get it right, please help. Thanks very much <3
$search = $_GET['search'];
if ($search == 'examp') {
header("location: examp.php");
exit();
}
You should check out the php documentation about header redirection.
php header
Also if you redirect to another page, your exit(); won't work because you're not on that page anymore.. I think you're missing the protocol start http/https://
edit:
It's just a guess, but aren't you looking for an include or require function? That incorporates your php file into the existing one and exits after exit() function. If yes check out:
Include
require
To say it simply, I am trying send a requested file to my user using a php request.
I did try to redirect my user directly to the file using
header("Location: http://myurl");
But, I am using Unity and apparently, this redirection doesn't act the same as if I go directly to the right url.
I get this error : necessary data rewind wasn't possible
Do you know another way I could send my files from the server to the client ? maybe with an "echo" ?
Thank you
header('Location: http://www.example.com/');
Or using javascript
<?php echo '<script>window.location="yoururl";</script>;' ?>
Finally, I did it with a simple
readfile("$ResourceId");
Thanks for your help, it makes me found it :)
What I am try to do is use $_SESSION['user_id'] to check if 'user_id' is = to "number", say 56 for example, if so load page, if not redirect user to "billing/".$_SESSION['user_id'].".php";
So far I have this
<?php
if ($_SESSION['user_id']) === 56) {
//do nothing
} else {
header("Location: billing/".$_SESSION['user_id'].".php");
exit();
}
?>
I know this code is wrong but hopefully it conveys what I am trying to accomplish.
Thanks in advance for your help and code snippets.
do not edit the code in your question based on the answers. You are making it impossible to understand what are you talking about.
If you want to add something - ADD it below the original text.
Ask clear, certain question. Describe the problem you face and what kind of solution you need.
Separate matters. As a matter of fact, sessions has nothing to do with redirects. If you want to know how to use sessions - ask how to use sessions. If you already have valid and verified session variable but have no idea of redirects - ask about redirects. If you don't know how to compare values - ask it. If you know everything but not certain about some bells and whistles of the code styling - ask this particular question.
Now, what is your question?
You're pretty close, but you could use the php function http_redirect instead for the redirect, to be more concise (requires the PECL library):
<?php
if ($_SESSION['user_id'] == 56) {
//do nothing
} else {
http_redirect("billing/".$_SESSION['user_id'].".php", array(), true, HTTP_REDIRECT_PERM);
}
?>
The PHP Doc for header() gives a lot of info about how to use the header function. The most important is:
Make sure no html or text has been echoed or sent to the browser before you call header().
You need an exit(); after header() since you're done rendering the page and some browsers prefer it.
HTTP/1.1 requires an absolute URI as argument to ยป Location: including the scheme, hostname and absolute path so you'll want to put the full URL after 'Location:'. See php doc for example.
Why not check the opposite? And for readability it is wise to use {} not "." (as long as your editor highlights this)
<?php
if($_SESSION["user_id"] != 56) {
header("location: billing/{$_SESSION['user_id']}.php");
die();
}
?>
Hi in a simple page i use php and javascript redirect to return to referrer page.
header("Location: $refererScript");
onclick="window.location.href='<?=$refererScript?>';"
Which is the best way to protect those scripts from generate errors:
Ex. should i use urlencode for $refererScript (or at least for query string ) and if so will this acceptable from javascript or must use escape (or something else)
For $refererScript i use the code above
$ref=$_SERVER["HTTP_REFERER"];
$refererParts = parse_url($_SERVER['HTTP_REFERER']);
$refererQuery=$refererParts["query"];
$refererFolders=explode("/",$refererParts["path"]);
$refererScript=$refererFolders[sizeof($refererFolders)-1];
if($refererQuery!="")
{ $refererScript.="?".$refererQuery; }
Thanks
I would suggest you to use php header approach because if javascript is disabled, then there will be no redirect and you should url encode it eg:
$refererScript = urlencode($refererScript);
header("Location: $refererScript");
In the $_SERVER["HTTP_REFERER"]; should be already valid URL. If not, someone changed it manually and will get redirected to the wrong page.
I don't see any security risks here. Your code is fine.
Possible Duplicate: How to make a redirect in PHP?
Hi!
How do i forward a page on the best way? Should I use the header-funct. or should i use HTML (meta-tags) to refresh? I hope some experts could give me some advice at this point. Thanks!
Btw, the forwarding is made inside an if-statement if that could be to some problem?
If you want to redirect the user to an URL, you can use the header function to send a Location HTTP header :
header('Location: http://www.example.com/new-url.php');
die;
(In theory, you should use an absolute URL that includes the domain name -- but most browsers accept a non-absolute URL)
You can use this wherever you want in your script, even inside a if-block, of course.
The only thing is, as you are setting an HTTP-header : you must not have sent any kind of output before (not even a white space at the end of an included file).
You can use JavaScript.
echo "<script>location.replace(\"$url\");</script>";
Take a look at https://www.w3schools.com/howto/howto_js_redirect_webpage.asp .