Calling a variable from another page in php - 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].', ';
}

Related

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'];
}

PHP create session for later PHP page

I want to save some values to the $_SESSION variable, I tried to create sessions like this:
if(isset($row_WADAsarenewals['AgreeNum'])) {
$_SESSION['AgreeNum'] = $row_WADAsarenewals['AgreeNum'];
}
But when I try to display this session like this it does not show up? echo($AgreeNum); What am I doing wrong?
To echo your session you will need to call the session-variable, not a regular variable with the same name as the the session-variable. So your echo would be:
echo $_SESSION["AgreeNum"];
Also, if you are having problems writing to your session, you might have to call session_start() prior to writing anything to your session.
You need to use
session_start()
at the beginning of your new script.
You need to use session_start(); prior to store something on the $_SESSION.
Then on the page you want to display the stored values, resume the session by calling again the session_start(); function. And retrieve the stored data like $AgreeNum = $_SESSION['AgreeNum'];
A call to echo($AgreeNum); should output the stored value.
You need to do;
echo $_SESSION['AgreeNum'];
Your solution just work when you have http://www.php.net/manual/en/security.globals.php turned on, which is not recommended. Because then $AgreeNum can came from $_GET for example.

Pass URLs between php pages

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'].

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

Categories