How do i get a link to run a Php script? - php

Hey, does anyone know how i cant get a href link too run a PHP script? Like the href dosent change page just runs a PHP script above? Any pointers would be great :))

Use Ajax to make the request to the PHP page in the background.

If you link to a script which, after executing some code, tells the browser to redirect back to your main page, you will achieve that.
example:
<?php
//some code...
header("Location: $_SERVER['HTTP_REFERER']");
?>
It's one way to do it. It doesn't work on every server.
Alternatively, you can hardcode your url, like:
header("Location: path/to/the/page/you/want/to/go/back/to");

...

Related

Php Redirect to a file

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 :)

Get Page URL In Order To Use It To Include

So I made a script so that I can just use includes to get my header, pages, and then footer. And if a file doesnt exist a 404. That all works. Now my issue is how I'm supposed to get the end of the url being the page. For example,
I want to make it so that when someone goes to example.com/home/test, it will automatically just include test.php for example.
Moral of the story. How to some how get the page name. And then use it to "mask" the end of the page so that I don't need to have every URL being something.com/home/?p=home
Heres my code so far.
<?php
include($_SERVER['DOCUMENT_ROOT'].'/home/lib/php/_dc.php');
include($_SERVER['DOCUMENT_ROOT'].'/home/lib/php/_home_fns.php');
$script = $_SERVER['SCRIPT_NAME']; //This returns /home/index.php for example =/
error_reporting(E_ALL);
include($_SERVER['DOCUMENT_ROOT'].'/home/default/header.php');
if($_GET["p"] == 'home' || !isset($_GET["p"])) {
include($_SERVER['DOCUMENT_ROOT'].'/home/pages/home.php');
} else if(file_exists($_SERVER['DOCUMENT_ROOT'].'/home/pages/'.$_GET["p"].'.php')) {
include($_SERVER['DOCUMENT_ROOT'].'/home/pages/'.$_GET["p"].'.php');
} else {
include($_SERVER['DOCUMENT_ROOT'].'/home/default/404.php');
}
include($_SERVER['DOCUMENT_ROOT'].'/home/default/footer.php');
?>
PHP by itself wouldn't be the best choice here unless you want your website littered with empty "redirect" PHP files. I would recommend looking into the Apache server's mod_rewrite module. Here are a couple of guides to get you started. Hope this helps!
The simplest way would be to have an index.php file inside the /home/whatever folder. Then use something like $_SERVER['PHP_SELF'] and extract the name if you want to automate it, or since you are already writing the file yourself, hardcode it into it.
That however looks plain wrong, you should probably look into mod-rewrite if you are up to creating a more complex/serious app.
I would also recommend cakePHP framework that has the whole path-to-controller thing worked out.

How to go to new Html page from PHP script?

After the php script is over, how do I go to another html page?
Why would you want to do that? When the page is over (which I understand as the script ended execution), it is usually sent to the client and then it can be viewed by the user. So, basically, what you are trying to do is to redirect the user after the script stopped executing.
If so, you have two solutions available:
Do not output anything, and after your script stopped executing, use the header() PHP function:
header('Location: http://example.com/some/url');
where you should replace the example URL with your own.
If you are outputting the HTML page and sending it gradually to the user, you can just put JavaScript redirection script at the end of the page (so after everything has been sent):
<script>
window.location = 'http://example.com/some/url';
</script>
Does any of these solutions work for you?
header('Location: /page.html');
Make sure you don't output anything else, then simply
header('Location: http://www.example.com/');

Executing only the code of a page?

Hi guys consider I have a .php page that I need to include somewhere else.
page.php:
<?php
dosomecmd();
doothercmd();
//etc
?><html>pagehtml</html>
Is there a way to include this page but not printing the html inside? (I just need to execute those command)
I could do with ob
ob_start();
include('page.php');
ob_discard();
But i would prefer a faster way.
Thanks
Edit: i know i can sepearate html and php (and i already do) but that page.php is non else than a "static" cache I make, but sometimes I need to execute some command inside that cache instead to printing automatically it out
Edit2: of course i don't need everyime to not output the html (otherwise I could just delete all html) I need to return; only based on the results of my cmds up there
Thanks all i find a solution (add return;)
Would it be easier to have a common page with only the php that you need to share, and include it in both page.php and your other page?
you can try to seperate with
$_SERVER['REQUEST_URI']
also I wouldn't recommend that
Try to separate the PHP logic at the start of your current script into another file, then include that into both of your scripts.

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.

Categories