views:

150

answers:

7

Hi there, I was wondering if there was any way through php or javascript I could tell the browser to go back to the page it came from, or even better not load the page at all (the later being probably impossible).

The reason for this is that I have written a small php script that will take parameters from the url and post a tweet for me discreetly while I am at work.

ex.

tweet.php?user=myname&pass=mypass&message=My message goes here

Though it works, I get stuck with a white page. It would be nice if I could have the browser go back to the page it was just on, so the pause between work would be minimal.

Thank you for the help!

+8  A: 

javascript: history.go(-1);
mgroves
Or...history.go(-1); within script tags.
mgroves
+2  A: 

You could do the following in PHP to redirect back to the previous page:

<?php
$ref = $_SERVER['HTTP_REFERER'];
header('refresh: 10; url='.$ref);
?>
James
Clever. Be aware, though, that `Referer` isn't 100% reliable and that this will lose, e.g., form field contents (which are normally preserved when going back/forward), because it will count as a new page load.
Ben Blank
To be honest I don't think this would be an issue for this particular scenario! But good point all the same.
James
A: 

If use use a PHP HEADER, you can redirect to another point on the site. Minimal Pause work (as long as the process isn't very long).

Noctrine
+2  A: 

Depending on the browser, either an HTTP response code of 204 or 205 might cause it to not leave the current page.

rmeador
204 is the way to go here.
EricLaw -MSFT-
+3  A: 

The JavaScript function for this is window.back(). Have your PHP script produce something like the following to have browsers automatically "bounced back" to the submitting page:

<html>
    <head>
        <title>Success</title>
    </head>

    <body onload="window.back()">
        <h1>Success</h1>
    </body>
</html>

Non-JS browsers will see a "success" message, JS browsers will get bounced back.

Ben Blank
Thanks so much. This seemed like the most "complete" answer to me.
Eric Koslow
A: 

In tweet.php, use the header function to redirect back to the referer

David Hedlund
A: 

Sending people back to the page without any success message would be very confusing. I would make the call with AJAX and provide some feedback to the user that the action was performed successfully, very much like the voting system here on SO works.

soulmerge