Convert .NET to php. Response.Redirect - php

OK so my goal is to get this page:
http://www.orchidfilmcompany.co.uk/Payment.aspx
to work in my php wordpress page.
I dont really know where to start, my whole site is ready to go except for this new pay online page.
The guy who created the .NET page has provided me with the Response.Redirect code which has the merchant peoples url with instID etc. The user will be redirected to this url to complet the payment
I have been looking around online and I found the the equivelant code in php for this is:
Header("Location: $url");
My problem is I dont know what to do with that?
Thats all I need is input box where the user can enter the amount they want to pay, they press submit and it redirects them to the url that I have in the Response.Redirect code. Uses the amount that they entered in the box and they can complete the payment.
If anyone could assist I would really appreciate.
Thanks in advance.

header() function is used to redirect the browser to a specific location.
If you already have the URL where you should redirect the client and you need just to add some amount that came from an input .. you should append that amount in the redirect url
eg:
purchase.html - include this form on your page
<form method="post action="/redirect.php">
<input type="text" name="amount" value="" />
<input type="submit" name="sumbit" value="Purchase" />
</form>
redirect.php - put this file next to your html file
<?php
$amount = (int) $_POST['amount'];
$urlToRedirect = 'https://secure.wp3.rbsworldpay.com/wcc/purchase?instId=XXX&cartId=OFMaterial&currency=GBP&amount='.$amount;
header('Location: '.$urlToRedirect);
exit;
?>

The form should have an action assigned to it, which is the page that will parse the form.
On that page (or within your parsing bit of the code), make sure that the redirect happens there.
The header statement is correct, e.g.:
<?php
header('Location: http://www.example.com/');
exit;
?>
Would redirect to example.com. So, construct the URL you want to redirect to there.
Please note: header directly modifies the headers returned by your webserver and therefore it cannot be called if you already sent other (HTML) output to your browser. Also see the documentation on header here: http://php.net/manual/en/function.header.php

You could try having a form which submits to a PHP page
The PHP page could then pick up the form variables using $_POST['FORM_VAR']
Build a $url variable from the submitted variables + .net page url
Finally use the header("Location: $url"); to take in the built url and redirect.

Related

HTTP_REFERER sends me to the same page

I'm using php and I'm trying to add a functionality to my contact form that sends me the page that precedes the contact page.
I added this line of code on my form code:
$httpReferer = isset($_SERVER['HTTP_REFERER']) ? $_SERVER['HTTP_REFERER'] : null;
$e_body = "Provenance : $httpReferer ";
This works fine and shows me a link, but the problem is that the link is the same contact page (http://...contact.php).
What I need is the page that the user visited just before getting to that contact.php page.
Anyone knows why is this happening?
Is there a way to go back 2 pages instead of one?
Thanks :)
One way to have a back button, is to add a hidden input to your form (seeing that is what you are using in a contact page), and using $_SERVER['REQUEST_URI'] as a value and assigning a session array to it and using sessions. Cookies could also be used, but I've used sessions in my example.
First start the session:
<?php
session_start();
// you can uncomment it to destroy previously set sessions
// session_destroy();
$_SESSION['back'] = $_SERVER['REQUEST_URI'];
?>
<form action="page2.php" method="post">
<input type="hidden" name="goback" value="<?php echo $_SERVER['REQUEST_URI'] ?>">
...
</form>
Then on the second page: page2.php
<?php
session_start();
// Your POST information for your contact code goes here
echo "Go back";
?>
Or, to have the full http call:
$link = "http://" . $_SERVER['SERVER_NAME'].$_SESSION['back'];
echo "Go back";
Sidenote: As I stated in comments,
The only way I know to go back 2 pages is with javascript:window.history.go(-2) which you could implement that in PHP with an echo.
Another way would be to use a breadcrumb method, but that would require some fancy work.
$_SERVER['HTTP_REFERER'] isn't fully reliable.
Read: https://stackoverflow.com/a/6023980/ as to why.
You could also use a header to redirect, but make sure you're not outputting before header.
Ref:
http://php.net/manual/en/function.header.php
How to fix "Headers already sent" error in PHP
References:
http://php.net/manual/en/features.sessions.php
http://php.net/manual/en/reserved.variables.server.php
Footnotes:
What I need is the page that the user visited just before getting to that contact.php page.
If there wasn't a referer to the page, then there is no way for a user to go back, because there is nothing to go back to, unless they use their browser's back arrow button.
A referer is a link that a person would have clicked from, either from your site or another.
If there was no referer, you can use javascript:window.history.go(-1).
If you really want to use $_SERVER['HTTP_REFERER'] (see other comments), then store this in a session when the contact form is loaded so you can use this when the form is submitted.
// Store the referring URL when loading the contact form.
$_SESSION['ref'] = $_SERVER['HTTP_REFERER']
// Redirect user back to referring URL when submitting the form.
header('Location: ' . $_SESSION['ref']);
unset($_SESSION['ref']);
die;
Hope this helps.
header('Location: '.$_SERVER['HTTP_REFERER']);
die;
Hope it will help you

PHP - Redirecting to previous page after logging in AND INCLUDING GET DATA

Okay so first off, sorry if this is a duplicate. I've searched for around 20 minutes all over stack overflow AND the internet, and I can't seem to find a solution to my problem.
What I'm trying to do:
I have a login form on every page, with a hidden input containing the current page the user is on - so when they login, it successfully redirects back to the page they were on.
E.g. User is on news.php and not logged in. They login, which takes them to login.php to verify data, then redirects back to news.php
This works great!
The problem: If there is any get data or anchor tags at the end of the URL, I can't seem to redirect back to that.
E.g. User is on news.php?id=4#comments and not logged in. They login, etc etc, but it redirects back to news.php and ignores the trailing data.
Anyone have any help here?
My code:
<input type="hidden" value="<?php echo $_SERVER['HTTP_REFERER']; ?>" name="location" />
$previousPage = $_POST['location'];
header("refresh: 1; url=".$previousPage);
Obviously I think the issue is the $_SERVER['HTTP_REFERER'] part, but I'm not sure what to replace it with to make it include trailing data.
All help is appreciated!
Just use
print_r($_SERVER);
and select the one that fits your needs most!
Hint: Combine HTTP_HOST and REQUEST_URI.
The hash portion will actually never be send to the server: Can I read the hash portion of the URL on my server-side application (PHP, Ruby, Python, etc.)?
Thanks to #Tobias,
This is what I ended up with:
<?php echo basename($_SERVER['REQUEST_URI']); ?><script>document.write(window.location.hash); </script>
Which on a page like example.com/news.php?id=2#comments will return:
news.php?id=2#comments

get current URL and previous URL when user pushes button

Newby here.
Could someone show me an example of the code needed to do the following:
User pushes a button on my web site (there is no information for him to input, and no form, he just clicks on a button). I have found the following code on another post, but don't know if it is correct (I am also getting a syntax error on it):
<form action="php_file.php"><input type="submit" value="Click"></form>
The author of the above code said "Insert your PHP-Code into the file php_file.php and click the button, your file will be opened. Insert header("Location: html_file.html"); at the end of your php-file to get back to the page."
This click of the button needs to instigate the programming to grab the current URL and previous URL and insert them into the mysql database on my server. I have "PHP_SELF" and "HTTP_REFERER", but still need to get the results into mysql.
I would like to do this using only html, PHP and mysql, if possible.
Thanks to everyone for any help!
if your first file happen to be a PHP one, write this HTML form there.
<form action="php_file.php" method="POST">
<input type="hidden" name="previous" value="<?=urlencode($_SERVER['REQUEST_URI'])?>">
<input type="submit" value="Click">
</form>
and then in the php_file.php
<?
$current = $_SERVER['REQUEST_URI'];
$previous = $_POST['previous'];
though both variables will contain only partial url, without host name, schema and, possible, port. it's usually enough but if you need these absent parts, you'll have to add them manually.
as for the writing info into database and particular PHP syntax rules you have to find yourself a tutorial, because this site is devoted to answering questions, not online education nor doing someone's job for free.
With PHP, you can manage it with cookie session, first thing you'll need to do is start a session and then define the space where you'll store the URL information e.g: $_SESSION["url"]
session_start();
$_SESSION["url"]=$_SERVER['REQUEST_URI'];
And whenever you want to go to that particular page, add the header:
header('location: ' .$_SESSION["url"]. '');
Current:
$currentUrl = $_SERVER["PHP_SELF"];
Previous:
$previousUrl = $_SERVER['HTTP_REFERER'];
Note that some users may have browser preferences set that keep $_SERVER['HTTP_REFERER'] from being set, so it's possible that it would come back empty.

Refreshing page will re-send form data, is it possible to prevent this? (PHP)

I'm creating a form and using it to get data input to send to a MySQL database via php. If someone hits refresh on the page Firefox ressends the last set of information to the php page which in turn sends it to the database. Is there anyway to prevent this?
To fix that problem, there exists Post/Redirect/Get pattern you need to follow :)
Post/Redirect/Get (PRG) is a common
design pattern for web developers to
help avoid certain duplicate form
submissions and allow user agents to
behave more intuitively with bookmarks
and the refresh button.
You need to do a redirect to the same page:
$current_url = (empty($_SERVER['HTTPS']) ? "http://" : "https://") . $_SERVER['HTTP_HOST'] . $_SERVER['REQUEST_URI'];
header ('Location: ' . $current_url);
exit ();
The usual way to do this is to use a redirect.
You get the request, use the data it contains to load your database or whatever, and then perform a redirect (I think you're supposed to use a 303 redirect for this, but I've heard of a lot of people using 302s to avoid certain browser glitches).
The net effect of this is that there was no POST data sent when the redirect occurred, so refreshing can't cause it to be resent and screw up your application/database.
If you don't like any of the above and are using JQUERY. You could do a simple load or ajax function to send the information to your script.
This will erase any chance of duplicate sending and you no page reload. I like this method best, it's fast and easy.
Another solution you can do is have your form send to another page, a bit like this:
<form action="chat_server.php" method="post">
Message: <input type="text" name="message" />
<input type="submit" />
</form>
On the chat_server.php file, you do what you need to do with the data and at the end, you do
echo '<meta http-equiv="REFRESH" content="0; url=chat.php" />';
Give it a try, should get rid of your problem.
Yes. After inserting data you do a redirect.
use a code in a hidden input and this code getting by a table codes for exmaple and if the code sending remove it from database and if the code not set in the table dont accept the query

HTML Redirect To Original Page

I have a one page site that has PHP code in it. Once the user presses 'Send', this sends the information to my email, then displays a messagebox saying that the action was a success to the user - great.
After the messagebox is closed, the website stays at website.com/report.php. Is there a way to redirect it back to the original page.
Also, any way to change the icon in the messagebox that pops up? Here is the code that I have:
<script language="JavaScript">alert("Your request has been sent. I will contact you soon!");</script>
Thanks.
Look into window.open and window.location
Place it after your alert()
http://www.tizag.com/javascriptT/javascriptredirect.php
Also, to answer your messagebox icon question: No, it is browser-dependent and not modifiable.
If you want to do that, your are going to need to fake it with html/css and javascript.
<script language="JavaScript">
alert("Your request has been sent. I will contact you soon!");
window.location.assign("http://website.com");
</script>
If you want to change the icon in alert box or make it look a little fancy, you could try YUI dialog
Use this code to display the alert:
function displayAlert(message, redirect) {
alert(message);
window.location.href = redirect;
}
Then, you can use code like:
displayAlert("This is the message", "http://redirect.the/user/here");
Use the php header command
<?php
header("Location: http://www.example.com/");
exit;
?>
To do a redirect in PHP, use header("Location: page.php"); for this. Before and after this your code shouldn't be sending any other output to the response. Eventually use exit(); to terminate the script afterwards.
If you need the page which was requested right before this page, then best what you can do is to include its URL as request parameter of the link to report.php and use it as redirect destination. E.g.
report
and in the report.php pass it as hidden input field:
<input type="hidden" name="referrer" value="<?php echo getParam("referrer"); ?>">
And after submitting the report do:
header("Location: " . getParam("referrer") . ")"; // getParam() returns sanitized GET parameter.
An alternative is to use the $_SERVER['HTTP_REFERER'] header (yes, including the typo) for this, but this is just not that reliable as it may be disabled or spoofed by the client.
I believe a more elegant solution is to simply present the user with a confirmation page (instead of an alert box), and place a link to the previous page there.
That would at lease work for all users.
For those users with javascript a little Ajax (jQuery) could submit the form for you, and display the confirmation nessage. All without leaving the page the user is on (negating the need for any fancy redirects).
Towards the end of your php, use this:
header('location: home.php');
This will cause the browser to load the original page.
I don't believe that the standard alert box can be altered, aside from the message. You can't change the title or the buttons, either.
Frank

Categories