Pass URLs between php pages - php

I was wondering how I can pass a URL to another web page using PHP. I know if I were to use an HTML form I could have a hidden field with the value being the URL I want to get.
How can I pass the URL along to another page without using a form?

If the page is your own, you could use a session variable.
More details here: http://www.w3schools.com/php/php_sessions.asp

You can use $_SESSION, or a cookie.

PHP has a server global variable which includes a referrer variable.
$_SERVER['HTTP_REFERER']
http://php.net/manual/en/reserved.variables.server.php
You can echo this and it will output the page which the user came from.

You could pass it using a get query,which is in a way form data, so i won't go into it
OR a session variable. On the sender page
session_start();
$_SESSION['url']="http://site.com"
and on the receiver
session_start();
$url=$_SESSION['url'];
unset($_SESSION['url'])l

You can initiate sessions.
Put session_start(); at the top of every page (normally by including a header or function). The session_start(); needs to come before any HTML output.
Then on your referring page, you can save values into the session array:
$_SESSION['URL'] = "myurl.com";
On your second page, you can reference it just like any other variable:
echo $_SESSION['URL'];

Link
somepage.php would then receive the URL in the variable $_GET['url'].

Related

Calling a variable from another page in php

I have such a loop:
for($i=0;$i<count($dersler);$i++)
{
echo $dersler[$i].', ';
}
I want to run this loop on another page by capturing $dersler variable from another page. How can I manage it?
Thank you.
You could pass the variable as a query parameter and access it again using the $_GET superglobal. An other option is posting the variable to the page using a form, and accessing it through $_POST.
But the solutions mentioned above require sending the value to the client side where it could be modified. If this is not desired, you could use a session to store a value serverside, between requests from the same client. Take a look at the session documentation: http://www.php.net/manual/en/book.session.php.
You can store $dersler in a session.
on the first page :
session_start();
$_SESSION['dersler'] = $dersler;
and on the other page :
session_start();
$desler = $_SESSION['dersler'];
for($i=0;$i<count($dersler);$i++){
echo $dersler[$i].', ';
}

Pass a variable value from one php page to another

I'm trying to send the value of this php get_field variable from a wordpress Archive page.
get_field('supplier_email_address', 'product_brand_' . $term->term_id );
to another sendmessage.php page which is never accessed other than with the of JS to send a form. The variable I need filled is called:
$sendto="value";
How can I achieve this?
EDIT: Apparently this requires more information.
Use session variables.
First of all every page you want to use sessions on needs to have the function session_start(); declared at the top of every page. You then can use the session superglobal to store things.
Pg1.php:
<?php
session_start();
$_SESSION['variable_name'] = 'string';
?>
Pg2.php:
<?php
session_start();
echo $_SESSION['variable_name'];
?>
Pg2.php will show: some string
Either POST your value so your second page can retrieve it in the $_POST array, or include it in the URL path so your second page can $_GET it.
Without more info, we can't really help you any more than that.

Session value doesnt pass on to the next page

Basically I want to grab an id send via the url (ex. www.website.com/?id=432432) and take it accross my website till the user hits the contact page. I created a variable and a session variable
session_start();
$getId = $_GET["id"];
$_SESSION['session_browser_test'] = $getId;
$adv_id = $_SESSION['session_browser_test'];
and used
echo $adv_id;
on my index.php Joomla template so it applies to all the pages.
But the issue is when i go to www.website.com/?id=432432 it echos the id on my web page, but if I click on the next link to go to another page (ex. www.website.com/nextPage) it doesnt hold the session value from the previous page. Why is that? and how can I carry the ID through out the site?
you will not get an id from URL on next page, likely
echo $getId;
instead you need to use id from session like,
$_SESSION['session_browser_test']; // your id stored in session
Start the session in each page
session_start();
In order to access the variable in a session, you have to call the $_SESSION variable.
echo $_SESSION['session_browser_test'];
HTTP is stateless, so you have to do something to remember your variable throughout the website .
make sure you correctly use session , like session_start();
when you send your id through get method ,it works, but when you go to any other page ,it doesn't make any sense to remember this.
use this for send id through pages:
<?php echo get_permalink(910); ?>?userid=<?php echo $value['userId'];?>
send this in url and use on next page as:
$sql = "select * from `wp_pelleresuser` where userId =".$_GET['userid'];
using this approach you can use a single variable on every page you want without using session. try google to how wordpress manage variable through all pages without using session. it will help you.
happy coding!
Start
session_start();
(if not started) in the index.php in root of your app (session probably will start on every pages) and then call (when desired):
$_SESSION['session_browser_test'];
instead of assi8gning this sess var to your own variable and then calling it in different places.
if(isset($_GET["adv_id"])){
$_SESSION['session_browser_test2'] = $_GET["adv_id"];
$adv_id = $_SESSION['session_browser_test2'];
}
else {
$adv_id = $_SESSION['session_browser_test2'];
}

Sending message without input elements

I want to send a feedback message from one page to another without using input elements via POST method. Is that possible? Or is there other way around?
What i want is applying a value from one page to another page's variable.
i.e.: Sending "Yaba Daba Doo" string from 1.php to 2.php's variable, named $info.
1.php:
<?php
$info = "Yaba Daba Doo";
?>
2.php
<?php
$variable=$_POST['info'];
echo "The message is:".variable;
?>
How can i pass info variable to 2.php using POST method, without input elements?
You can use session variable to store data on server and access across the pages of your application.
If you want a secure way to send this variable you can use session so that end user can not see it
An example
page1.php
session_start();
$_SESSION['mes']=$info;
page2.php
session_start();
echo $_SESSION['mes'];
And the another way is to pass variable in GET which I will not recommend user can see the value of your input variable
Use a session to store your data, then when ever you want to store is call upon the $_SESSION global.
Here is more info about sessions:
php.net
W3schools
Tutorial
you can use php curl to make a post request to 2.php from 1.php

passing session id via url

I'm trying to get my script to use url session id instead of cookies.
The following page is not picking up the variable in the url as the session id.
I must be missing something.
First page http://www.website.com/start.php
ini_set("session.use_cookies",0);
ini_set("session.use_trans_sid",1);
session_start();
$session_id = session_id();
header("location: target.php?session_id=". $session_id );
Following page - http://www.website.com/target.php?session_id=rj3ids98dhpa0mcf3jc89mq1t0
ini_set("session.use_cookies",0);
ini_set("session.use_trans_sid",1);
print_r($_SESSION);
print(session_id())
Result is a different session id and the session is blank.
Array ( [debug] => no ) pt1t38347bs6jc9ruv2ecpv7o2
be careful when using the url to pass session ids, that could lead to session hijacking via the referer!
It looks like you just need to call session_start() on the second page.
From the docs:
session_start() creates a session or resumes the current one based on the current session id that's being passed via a request, such as GET, POST, or a cookie.
EDIT:
That said, you could also try manually grabbing the session id from the query string. On the second page you'd need to do something like:
ini_set("session.use_cookies",0);
ini_set("session.use_trans_sid",1);
session_id($_GET['session_id']);
print_r($_SESSION);
print(session_id());
Note that the session_id() function will set the id if you pass it the id as a parameter.
Instead of hardcoding 'PHPSESSID', use this:
session_id($_GET[session_name()]);
My issue was using Flash in FF (as flash piggy backs IE, so sessions are not shared between the flash object and firefox)
Using php 5.3 all these answers pointed at the truth. What I finally found to work was pretty simple.. pass the id in the query string. Set it. THEN start the session.
session_id($_GET['PHPSESSID']);
session_start();
Just a little correction ...
Don't forget to check param if it exists.
This worked for me well.
if (isset($_GET['PHPSESSID'])) {
session_id($_GET['PHPSESSID']);
}
session_start();

Categories