httprequest getting back PHP functions and variables - php

How does one access and receive back a variable from a PHP file with a HTTP Request?
I have gotten a HTTP Request to connect to my .PHP file. What I wanted to do next is for example reference that I want to receive $testvariable = 1; back but I have no idea where to begin. The HTTP parameters don't really let me reference this $testvariable directly.
What if the PHP file has simply one function and at the end of it it does a return $testvariable;? Would HTTP receive back this one variable? What if I need more than one. Maybe try and get the PHP to place parameters in the URL and the HTTP reads those parameters in the url? Maybe these "headers" are key to this...

I figured it out. HTTP Request gets back ALL that is on your HTML page. It seems like it ignores <html><body> tags but everything else in the body gets taken back at your response. Even if your answer is " 1 " but you have a single space around it your response is 1 including blank spaces. You need a clean "1".
Passing variables works by using the contents field. You place your values and assign them to a string like number= that then gets posted on the page. When a page is loaded, it can look for a variable under the same name like $test = $_POST['number']; and take the value within that variable and use it further where it needs to on your page.

Related

Include a php file twice?

I am working on a page that demands that a certain php code is included at runtime - however, at some point in the code below, the variable in that included file is to be rewritten by a remote curl POST request, so I need to re-include the file to read the new value of the variable.
Can I "include" it again to reload the new value? Or is a double include of the same file within the same code not allowed?
EDIT:
Here's what I'm doing exactly:
include a file, that contains 1 variable
run a check in an online API to make sure the URL from the variable is not in the database
if it is, initiate a cURL POST request to a page on my second domain, that starts a chain of events there
after that chain is completed, the second page sends a cURL request to another page on my first domain - the request contains another URL which it passes to a page on my first domain, which in turns grabs that and overwrites the initially included file with the new value for the variable
back to my initial code - I now have a new value for that previously included variable, so I need to "reload" it somehow, because I will be using it a bit later in the code of the same page; won't re-including the file be best?
I think you should move the "later part" of the code in your first PHP file to the file that overwrites your initial variable in #4. Else, move that code to new php file altogether.
You can also implement a web-hook kind of system where you'd pass the "variable" to the first code as a GET/POST parameter. So, the first time that code gets called it will check for the variable. If empty, then it does what you mentioned in steps 1-4.
Then step #4 calls that PHP code again, but passing a variable value. Hence, instead of executing the first part of the code, the file executes the later part.
In any event, your code seems to be too convulated, and is best to split it into functions and classes or something.
I ended up using the following:
$newly_updated_file = "file_with_variable.php";
$lines = file($newly_updated_file);
$new_variable = $lines[15];
Ought to do fine.

how to check the response header after sending the modified request

I was thinking if their is any way i can check the response header or the page is rendered differently after i have modified the request header or get parameter.
Like:
Original:www.abc.com/index.php?a=2
mODIFIED:www.abc.com/index.php?a=2polo
Now when i have appended the polo string after 2 i should see page rendered differently or it should rendered normally. But how can i check that it was rendered normally or not.
Any way i can compare normal response and the modified "request " response?
Elaboration of my problem.
I have an example.
suppose this is the original url:http://www.lnctgroup.in/institute.php?institute=4
Now this is the modified one on get parameter:http://www.lnctgroup.in/institute.php?institute=4polo
You can see the page rendered normally in original case but while appending "polo" string it shown error as mysql error may be but the page rendered differently.I just want to compare if i am getting the same page or different page after i have modified the get post parameters.
Help
If you would have gone through this then you shouldn't have asked this question.
http://php.net/manual/en/class.httpresponse.php
Go through it first.I cant prepare fully functional code for you here.

javascript post to .php file one server does not work - manual entry of URL in browser to same .php works

i had some issues with understanding how to get javascript (client) variables transferred so they were acessible from php (serverside) as session : get an iframe's "src" value in PHP?
Now im in a situation where i use firebug to try to debug whats going on, but it just doesnt make sense :
i have this function to update an iframe and i want to pass on the page that that iframe is displaying :
function frameclick(pageurl)
{
$.post("session_write.php?",
{
frameurl : pageurl
}
$("#iFrame1").attr('src', pageurl);
console.log ('<?php echo "logout:".$langpath.$_SESSION['frameurl'];?>');
}
pageurl is ex. "/lang/en/new.htm" - and if i inspect it with firebug i also can see it says that it passes it correctly ( also with conversion of /).
my script serverside that its posted to is like this :
#session_write.php
<?php
session_start();
print_r($_GET['frameurl']);
if (isset($_GET['frameurl']))
{
$_SESSION['frameurl'] = $_GET['frameurl'];
print_r($_SESSION);
}
?>
Posting to that php script on the server will fail via the javascropt - $_SESSION['frameurl'] will be '', but if i ex. do it manually like this :
(http):
//localhost/phpmenu/session_write.php?frameurl=lang%2Fen%2Fnew.htm
then it will be correctly set in the $_SESSION["frameurl"] variable.
I simply cannot understand whats different between doing the javascript post and doing it manually in the browser and why its causing me this problem ?
anyone with an idea ? thanks
You are using .post, which executes a POST request, but when you type in the URL in the address bar, that is a GET request.
$_GET retrieves any params passed through GET, while $_POST retrieves any params passed through POST. So if you use .post with Javascript but try to retrieve with $_GET in PHP, it wouldn't work.
When you POST variables to a PHP file, $_GET is not set. Use $_POST['frameurl'] instead. Also, it looks like you're missing a close paren in frameclick to end the post call.
You are passing data via a
POST request and retrieving for all the GET requests. Use $_POST instead. You may also be interested in $_REQUEST

Paste variables to $_GET

I have two pages. First one we open with $_POST variables in its url, the second one opens inside first via iframe. Both php files, second is for html manipulation.
Variables I got in $_POST are passed to iframe via $_GET:
echo '<iframe src="index.php&first=' . $first . '&second=' . $second . '&third=' . $third . '&iframe=true"></iframe>';
$first, $second, $third variables has text inside them with some html and new lines (\n).
The problem is, when data is passed to iframe by $_GET, all the new lines in variables disappear.
Tryed to pass variables like base64_encode($first), and then decode them by base64_decode(). It works buggy, some parts of text don't decode correctly, maybe because of bad symbols in iframe url.
Also tryed to throw all the variables into single array, serialize it and then encode by base64 - this way server gives error 500 (it also gives the same error for 404).
Please don't ask me why I did such structure of pages. It should not be changed.
What is the solution for this?
What about an urlencode after the base64_encode?
Depending on your situation you could also use Javascript to access the parent frame.
You could store the data in a javascript array of the first window, then the iframe sub window could call it via parent.*
Some more details from other questions.
You could write the contents of $first,$second,$third to first.txt,second.txt,third.txt and then open the text files inside your iframe script
Your initial approach is wrong.
POST variable shouldn't go anywhere.
After the POST request server have to order browser to reload the page.
Whole page, not only iframe in it.
After that reload you may show any iframes to user.
To pass the data there, a session would be ideal solution.
However, certain solution depends on the data nature and overall purpose of all the mess.

Unassigning a variables value from $_GET

I use;
$referrer = $_GET['_url'];
If I echo $referrer; it will display correctly.
If I use $referrer within a $_POST, it is empty. I think due to $referrer being assigned to $_GET.
How can I extract the value of $referrer into another variable so it is no longer assigned to the $_GET?
I hope that makes sense..
$_POST will only contain data IF you send that from form.
So, your code is basically right. Because you use referrer from within your URL.
If you really want to have $referer from $_POST, you will have to code something like this:
<form method="post" action="somewhere.php">
<input type="hidden" name="_url" value="{place the referrer here}" />
</form>
Or, like #Michael Gillette answer, you can change that with $_REQUEST.
hope that makes sense..
sorry, but it doesn't :)
$referrer become distinct variable with no relation to $_GET['_url']. It already contains value extracted from $_GET
there are not a single reason for $_GET sourced variables to conflict with $_POST.
Your problem is somewhere else.
It seems you're just trying to access variable that doesn't exist. Because every variable dies along with whole PHP after it's execution.
PHP scripts execution is atomic. It's not like a desktop application constantly running in your browser, and not even a demon with persistent connection to your desktop application. It's more like a command line utility - doing it's job and exits. It runs discrete:
a browser makes a call
PHP wakes up, creates an HTML page, sends it to the browser and dies
Browser renders that HTML and shows it to the user.
User clicks a link
a browser makes a call
another PHP instance, knowing nothing of the previous call, wakes up and so on
So, if you set your $referrer in one instance and trying to access it in another, it will fail. You have to re-sent it's value with next call
Use the clone keyword, as seen in the examples in this article:
http://php.net/manual/en/language.references.php
could you post an example of what you might like to do?
$referrer = $_REQUEST['_url'];
will return true on both GET and POST requests

Categories