Grab data from one PHP page and display it on another - php

I do not think that I am doing this the right way, but here goes it. I want to be able to have two php pages mypage.php and mypage2.php. mypage2.php has plain text data on it (say a 8 digit number) and it is just being displayed via the echo command.
I want to know if when I am on mypage.php if I can grab that number and display it on mypage.php rather than open up mypage2.php and display it there?
So a hypothetical example of mypage.php might be:
echo "My secret number is: " . magic_command("mypage2.php");
I know that one way to do this is with $_SESSION, but doesn't that involve still navigationg to mypage2.php, storing the number in $_SESSION. Then telling it to go back to mypage.php?

You can do:
include 'mypage2.php';
in mypage.php and in mypage2.php:
echo '12345678';
Easy as pie!

The way you're presenting your question makes me think that you're doing this the hard way. You could store the information in a variable, include the page, and echo it that way. However, I'm assuming there is more to it than that, in which case you're probably looking for a cURL, or even a file_get_contents solution. Now that I think about it, there are a few ways. Just have to do some research.

If fopen_wrappers are enabled, you can grab the output of one page into another with readfile(), like so:
$secret = readfile('http://server/blah/mypage2.php');
echo "My secret number is: $secret";

If that number is user-specific, you should probably store it in a database and then call it when needed.
Otherwise, if you absolutely want to store it in a separate php file, use include as yvesonline suggested. include acts as if the script in mypage2.php is now inserted into mypage.php.

Related

Php Session Variable as desired file name

I am having a dilemma.. my php wont grab the session variable
This is my session start, I would like to pass my variable to this 2nd page, so I can download the posted file name. Please advise
Just change the path to "$root\\loan_inquiry\\user\\$name.pdf", like
$path = "$root\\loan_inquiry\\user\\$name.pdf";
Note that this is very dangerous. You're using user input as a file name, which can go horribly awry in many ways (think, horrifically bad hacking). You'd be better using an ID number or a hash, not raw input.

Hide ?ref string in URL but pass it to script

How can I hide ?ref string from users but pass it to php code so it will grab it?
For example, visitor visits this page:
http://mysite.com/?ref=ref+string
In browser URL I want to hide it, so user will see"
http://mysite.com/
But I want to grab content of ref string via this:
$ref = $_GET['ref'];
Is it possible?
No, if you want to use GET variables, they will always be in the url.
However, you can use an alternative, like $_SESSION or $_POST variables.
You could try something like this at the top of your PHP script:
session_start();
if (isset($_GET['ref'])) {
$_SESSION['ref'] = $_GET['ref'];
header('Location: /');
exit();
}
You would have to use $_SESSION['ref'] to access the value from then on, however.
This is not how the http protocol works with query strings. If you have information that needs to be in the query string that you want to hide or obfuscate from the user, I would recommend a simple encryption to change it. If you're attempting to get rid of this information for aesthetic reasons, you will need to pursue a different method of generating the header or storing the information (session/cookies/etc).

php setting the query string

I have a form that submits to the same page. Now when it gets submitted and after it's processed I need it to have a unique query string.
So for example the user inputs some info then clicks submit, then the page saves the info on the server then the server spits it back out with a unique query string for that info.
If I try to set $_SERVER['QUERY_STRING'] it just hangs. Is there another way to do this?
Is it possible with a redirect?
EDIT, I'm going from mysite.com/ and the form action is on mysite.com/ and I want the browser to go to mysite.com/?blah
OK I tried putting this on my the top of my page with no luck
<?php
if ($_POST['data']) header('location: /?' . idFromDifferentFunction() );
?>
but it just keeps loading, I'm guessing it just redirects itself to death.
I hope you now understand what I'm trying to do
Chances are that your script is continuing to run after the code that says it should redirect. You also need to be more precise with the header:
<?php
if (isset($_POST['data'])) {
header('Location: /?' . idFromDifferentFunction() );
exit;
}
?>
If you use the code above, it will make the script exit which dumps the output and the browser will see the redirect (note the capital L in Location).
The key point is the exit following the redirect header. Without it, PHP is very likely going to continue working on whatever other code you're doing in the script.
It's not entirely clear what you're after, but I think you mean you want to go to a page with a unique value in the query string (the bit after the ?) once the processing is complete. Does this unique value need to actually reference something in the system (for a newly-created DB entry does it need to reference the ID of the new entry) or does it just have to be unique?
If it's the latter, you could just generate a random unique ID do the following:
header ('location: /path/to/script?id=' . uniqid ());
If it's the former, then replace the call to uniqid with the value of the database key.
The values in $_SERVER are set at runtime by PHP and should be considered read-only. Changing their values will have no meaningful effect.
$_SERVER['QUERY_STRING'] is part of PHP's globals. You should not be setting those variables, instead set it via a session and return it after submission.
If you are trying to redirect the user to a specific URL then use:
header('Location: mysite.com/bla/bla');
Writing to $_SERVER is pointless. It doesn't affect the client browsers in any way. If you want to change the query string displayed in the client browser, you'll have to use a 301/302 redirect using a header('Location: ...') call.

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.

need help in particular php job, dont know how to do it

I want to do something but cant figure out how to do this (i m newbie in php)
suppose, i have a list of URL's which shows live with this preg_replace,
$html = preg_replace('/\s(\w+:\/\/)(\S+)/', ' GO ', $html);
my output is like
http://localhost/get.php?url=http://yahoo.com
its obvious that you can view that links at output page, now i want to hide them at front page and make them clickable and working
something like we can change links into variables and then call them by clicking and something works in backgroud which can perform same thing as we are clicking on the link at front page
ya it seems bit confusing :(
you could save the url into a $_SESSION vars and when some users click the link retrive the url from $_SESSION and redirect to it...
//page1 - parse, save link in session and print a call to page2
<?php
session_start();
$_SESSION['url'] = preg_replace('/\s(\w+:\/\/)(\S+)/', ' GO ', $html);
....
echo 'GO';
?>
//page2
<?php
session_start();
header('Location: '.$_SESSION['url']);
?>
If i undeerstood what you meant...
Obviously now I used $_SESSION['url'] as a single string, but you can use a multidimensional array intead...
UPDATE:
anyway is better if you use an array on script..
example: http://www.test.org/go.php?page=# (where # is a number)
<?php
$array=("http://www.google.com","http://stackoverfloc.com","ecc");
//you can add more contorl in if statement, like between etc...
if (is_numeric($_GET['page']) header('Location: '.$array[$_GET['page']]);
?>
I'm sorry, maybe I'm not understanding quite well, but, isn't best approch use an array for in side server and use another Get variable to do that?
for example ?link=yahoo
and then
find link in array of url?
BY the way, I'm using NoScript and reports me like a warning..
You build up links with looking like this:
yourdomain.com/redirector.ph?url=#
where # represents an identifier.
In redirector.php you check if you know that identifier and send the redirect HTTP Header:
header("Location: http://www.example.com/");
Important Note:
You may not send any data before sending the header and the code after sending it won't be executed.
Info
http://php.net/manual/en/function.header.php
EDIT
Using this header is not absolutely proper in regards to standard, but it's not too far off, as the response really is at another location.

Categories