redirect to another page using header() - php

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

Related

Redirect PHP query

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
}

Would header statement work in inner code?

Please see code below:
<?php
require_once("initvars.php");
require_once("config.php");
if( !$auth->id ){
//NOT logged in
header("location: index.php"); die();
}
Yes, as long as you have not echo'd or produced any output in general.
Provided that you the included files haven't sent any output, yes, that should work.
Note, however, that you really should provide a full URL (Location: http://example.com/index.php) to header.
It depends on the contents of the included files. It should work provided the files do not send out any headers. Also see this function http_redirect.

php session id redirect

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();
}
?>

php and javascript redirect

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.

How to avoid Page Reload Resend-Data message with PHP

I am relatively new to PHP, so my apologies if the answer is trivial. Lol
I wrote a simple Contact Us email form (actually a WordPress page-template file). All the code is in one file.
After the user submits the form and the email is sent, the file generates a Thank You message.
If the user reloads the Thank You page, they are prompted to "Resend the Form Data," which is why I am asking this question.
My question: How do I avoid the prompt to resend the form data and still keep all of my code (including the Thank You data) in one file?
EDIT: I've seen folks use headers( Location: ), but I don't think that will work for if I want to keep all my code in one file.
You could redirect to a different query.
header("Location: ?page=thankyou");
Then, you don't even need to check if the POST data was sent. Just display the thank you page if page is equal to thank you.
This worked for me, it can be put anywhere in html file not just beginning like header() function:
<?php
if (!empty($_POST)){
?>
<script type="text/javascript">
window.location = window.location.href;
</script>
<?php } ?>
I placed it into prevent_resend.php and then included it after the postdata processing was done.
// ... save data from $_POST to DB
include('prevent_resend.php');
// ... do some other stuff
You can use javascript to post the form and show the thank you message. This way the browser never leaves the page.
Even with a header('Location: xxx'); you can still redirect it to the same page, and either set a url parameter or a session variable to distinguish what should be shown.
Although I question your requirement to have all the code in one file (why couldn't you separate it, and use require_once to include shared library code?), the header('Location:') technique is still completely valid. Simply do:
header('Location: http://www.example.com/path/to/my-one-file-of-code.php?thankyou=1');
Then, in your file, you can have:
if (isset($_GET['thankyou']) && $_GET['thankyou']) {
// Do whatever it is you do to thank the visitor.
}
This worked for me:
header("Location: #");

Categories