Open a PHP Page without redirect? - php

I have been writing a module for our billing system at work, and it works beautifully. Unfortunately the API lacks a function that I need to terminate services immediately leaving me with the only option of calling the URL directly at...
http://www.website.com/billing/admin/clientsservices.php?userid=69264&id=68405&modop=terminate
As the module needs to continue to run without redirecting to that URL, how can I execute that from my PHP script?

You could simply use file_get_contents();
<?php
file_get_contents('http://www.website.com/billing/admin/clientsservices.php?userid=69264&id=68405&modop=terminate');
?>
And it will be called.

Why not just use curl to POST the data to the URL instead of using GET?
http://php.net/manual/en/book.curl.php

Related

PHP how to pass `http request` object from one PHP file to another PHP file

usually when i want to redirect from one php page to another php page of same project i'm using
header("location:somepage.php");
This will cause more calls between client and server. What i want to do is instead of sending redirect header, i want to stop execution of requested page and pass request object or request information to the another page which i want to redirect. In this case single request will be enough. I guess this kind of functionality available in jsp. Is same thing available in php which i don't know?
As #DanSherwin commented, you probably want to use include. You might do something like this:
firstpage.php:
if(/* Some condition when you want to do a redirect */){
include 'somepage.php';
exit;
}
This runs the code from somepage.php immediately, as though it was cut and pasted into firstpage.php**, and then it exits right afterward as though you redirected away from firstpage.php.
** caveat: watch out for variable scope.

PHP load function not working

I am using formmail by tactite to have the info submitted from my form get emailed to me. After the user hits the submit button, it goes to a "Thank You" page that by default just has some text, I'm trying to change that to load up a thank you page that I created and it doesn't work, what am I doing wrong?
Thanks!
Here's what doesn't work:
// MSG_THANKS_PAGE is the default page that's displayed if the
// submission is successful
// Parameters: none
$aMessages[MSG_THANKS_PAGE] = load('http://nimbledesigns.com/kelsie/thankyou.html');
This is what i had there before that DOES work:
$aMessages[MSG_THANKS_PAGE] = 'Thanks!<br /><br />'.
'Go Back'.
'';
Tere is no load() function built into PHP. Most likely what you're looing for is file_get_contents(), which'll retrieve the contents of a file (local or otherwise) as a string.
If that URL points back to your own server, you may want to save yourself a full HTTP round-trip and simply use a local path ... = file_get_contents('/path/to/that/thank/you/file.html').
File_get_Contents()
use
$aMessages[MSG_THANKS_PAGE] = file_get_contents('http://nimbledesigns.com/kelsie/thankyou.html');
instead.
Documentation
file_get_contents() - http://php.net/manual/en/function.file-get-contents.php
Alternatives'
If that file is on your server, then you may only need to do this:
$aMessages[MSG_THANKS_PAGE] = file_get_contents('thankyou.html');
That will stop PHP from using the HTTP stream connector and will use the File IO connector instead, which is going to be faster with less overhead (although the difference may only be viewable when your server is running slowly)
Redirects
You could also redirect them to the page, by issuing this command before you send any data to the browser:
header('Location: thankyou.html');
exit();
This will redirect their browser to the file. Again assuming it resides on your server. You could replace that with a the full address if required http://nimbledesigns.com/kelsie/thankyou.html
As stated earlier, file_get_contents is your best bet. There is no load() function.
But why not just redirect to the page?
It says how to here: http://www.tectite.com/fmhowto/redir.php
(I'm assuming that's the form mailer you're using, and "tactite" was a typo).
haven't used php load for a long time, but isn't it just for xml and returns an object?
is this? http://php.net/manual/en/domdocument.load.php

PHP: invoking remote server from my php server?

how can I invoke a php script on a remote server from my server code ?
I'm currently using:
header('Location: http://www.url.com/script.php?arg1=blabla');
in my code, but it doesn't work.
thanks
If you mean by invoking just "calling" it, so you only need it to run, then you can use curl.
If you mean by invoking that you want it to act the same as include, then you can't trough http (the server does ofcourse not return code, but runs it). You might be able to obtain the file trough other means (ftp?), and then include it, but that seems like a bit of a hack.
If you mean by invoking that you want to redirect the user to the page, then this should work:
header('Location: http://www.site.nl/');
exit;
(your script continues to run after a header call, so you might need to call that exit). How doens't your code work for you? (I'm guessing because you want one of the other options)
If you only want to invoke the script you can simply use $result = file_get_contents('http://www.example.com/');.
Your version using header() will as said above redirect the user.
Use cURL, it gives you much wider manipulation options.

How to get the HTML source code after executing header(location:URL)

My web hosting provider does not permit to use curl FOLLOWLOCATION option so I'm trying to
do it manually by using the header function.
My problem is that I need to keep my PHP script running and to be able to get the redirected URL data for parsing.
How do I do that?
Technically the PHP script continues running after the header () function is called. How you get URL data is another question. Can you not use get_file_contents () or readfile () on the URL?
You read the RAW data the request returns, you check for the redirect header(s), fetch the related URL(s) and do a new get with that URL (dry, rinse, repeat). As simple as that...
Alternatively you could stop being so lazy, check the curl_setopt documentation in the PHP reference manual and find solutions - by reading the comments at the bottom of the page - on how to solve this problem of course.

Making file_get_contents() session aware in PHP

Preamble: My app is mod_rewrite enabled and I have index.php page that downloads vaious pages based on Request_URI and prints them as page content.
Problem: File() or File_get_contents() function is excellent at downloading my other app pages. But as soon as I try to use it to download a page that is session enabled, I start having problems.
The main problem is that when I try to pass existing session id to url from the page I download, e.g.
$url = "http://localhost/EmplDir/AdminMenu.php";
return implode('',file($url. "&" . session_name() . "=". session_id()));
My page never loads (or file() never loads content).
I suspect I shoud use curl functions here, but it has too many options.
My be an advice which curl options to use to make downloadable pages know about current PHP session would be helpful.
P.S. The above seems to be true both for Windows and Linux.
You didn't separate the query string from the rest of the URL with a ?
Try
return file_get_contents($url. "?" . session_name() . "=". session_id());
You will also need to be sure the server doesn't use the session.use-only-cookies configuration setting.
There's no reason why the script shouldn't see the query string and act on it, you can persuade yourself by writing a script which just does var_dump($_GET) and requesting that as above. If you see the query arguments in the output then you simply need to debug your script to see why it doesn't behave as expected given the session id.
NOTE: I'm assuming that you wanting to request a file from the same domain as your application, otherwise using your session id for a remote site doesn't make much sense.
If your script doesn’t alter any superglobal variables, you could just include it:
ob_start();
include $_SERVER['DOCUMENT_ROOT'].'/EmplDir/AdminMenu.php';
return ob_get_clean();
session_name and session_id gives you the current scripts session; Not the remove server. You need to use something that understands http. Curl would do, or you can use something like SimpleBrowser, which completely emulates a browser.

Categories