PHP access to global variables from an HREF='file.php' - php

I have a menu that is in an iframe. The user selects an option from the menu to log in and after the credentials are verified (using mySQL) (via a call to href='login.php') - I need to access several variables (such as the object handle to the mySQL connection ($dbconnection), user id ($userid) value. I have declared these variables as globals and they appear= to be inaccessible from the launch of href="other.php" menu option. I have tried to put these as $GLOBALS and the same results - they are inaccessible from the launch of href="other.php" menu option.
Passing these variables in the href call via the use of ? and & is not a good solution (and besides you cannot pass an object such as a Data Base handle. I need these variables in other files I open from the menu via the href.
What is the best way to have this functionality?
Jack

I found a solution to the problem. In order to pass variables to called PHP files via the HREF I had to put these variables in the SESSION global. I then did a $enc=session_encode() which encodes into a string all the user defined session variables and then I passed the string value to the called file via the use of the ? after the file being called. In the file that I called, I pulled the passed encoded SESSION string via a $GET and then used the value from the $GET to call session_decode (). That seemed to work. I am also thinking of hashing and salting this encoded string so that it is not easy to hijack it.

Related

How to create a global variable and use it in other pages?

I tried this :
in page1.php :
$GLOBALS['nb_ajout_client'] = "rrrr";
in page2.php :
$GLOBALS['nb_ajout_client'] .= " kkkkk";
I displayed the value of $GLOBALS['nb_ajout_client'] but it is kkkkk.
So how to create a global variable and use it anywhere ?
Global variables only survive the lifetime of the program execution.
Each time you load a (PHP) page, you run a program from scratch. If you load a different page then you run a different program.
If you want to store data between them then you need to actually store it somewhere and then read it back from there in the other program.
If you want to do this globally, then the usual approach is to use a database.
If you want to do this on a per-user basis, then the usual approach is to use a session.
You could also pass the data via the browser (e.g. by putting it in the query string of a link and then reading it back from $_GET).
A variable is only 'global' in the current script. If you want to use a variable from page1, you need to include it from page2.
To pass variables to other page (other request) use PHP Sessions
you can try this.
g1.php
<?php
$GLOBALS['nb_ajout_client'] = "rrrr";
g2.php
<?php
include('g1.php');
$GLOBALS['nb_ajout_client'] .= " kkkkk";
var_dump($GLOBALS['nb_ajout_client']);
$GLOBALS are global in all scopes throughout a script.
To pass the value to another page:
use $_POST
use Session (server-side )
use cookies(client-side)

PHP: Store PHP variable in page to use in POST

Currently I put a constant on my webpage using PHP which I then send, with Ajax, to my POST function. However, this leaves it susceptible to hacking (the user could change the variable with Firebug), so is there a way to store the variable in the PHP of the page and then access it later on, in the POST method (or is the GET variable of the page still available in the POST function, since that's where I get the variable from)?
I think what you have wanted is to store the post value to use it later.
Here you would need to use $_SESSION
You can do it like
session_start();
// Save variables into session
$_SESSION['thevalue'] = $_POST['value'];
If you wish to store between successive calls from the same user use the follwing:
<?php
session_start();
$_SESSION["your variable/constant"] = yourvaule;
Now use the variable as needed, accessing it as $_SESSION["your variable/constant"]
Hope it helps, and it's what you're asking.

How to access a variable from one PHP page to another, without a form, link, or button?

TLDR:- What is a good way to pass contents of a variable from one PHP file to another without involving a form, link or a button.
Question:-
So there is a form in a page/file called question_edit_form.php and its action attribute is already set to another file called question.php. The variable of interest is being read-in from the user in question_edit_form.php and is then obviously being sent to question.php using $_POST.
Now, there is a third file, named renderer.php, and which is not linked to the other two files. I want to use that variable of interest in this file. So how can I access that variable which is set in question.php from inside renderer.php?
first file -
session_start();
$_SESSION['your_variable'] = 'value';
other file -
session_start();
$var = $_SESSION['your_variable'];
this may help.
It sounds like you are using Moodle, in which case renderer.php is not an independent file; it contains the class definition for the renderer object used by question.php.
So... there is no need to pass the parameter between the scripts. If you really must access the form value directly from the renderer, just use the standard methods from the Moodle framework: required_param($name, $type) or optional_param($name, $default, $type).
Generally there are two methods available for you to pass on the value
Cookies
Sessions
How to use cookies:-
setcookie(name, value, expire);
e.g.
setcookie("user", "Alex Porter", time()+3600);
Access it using echo $_COOKIE['user'];
Second is Sessions. Here is how to use sessions:-
session_start();
$_SESSION['varname']=value;
Accessing page:-
session_start();
echo $_SESSION['varname'];
Additional info if required:-
Make sure you use session_start() at top of your page if not you may face with an headers already sent error / warning which again can be fixed by output buffering ob_start()
You can store the variables in the session.
http://www.w3schools.com/php/php_sessions.asp

Redirect and pass a value from django to PHP

I have a php code sending data to django.
I want django to redirect response to another php page instead of the previous one.
Here is my view which is called from PHP :
def search(request):
arg = { 'Score':clf.predict_proba(my_sample)}
response=redirect('redirected_url.php',arg)
return (HttpResponse(response)
query_arg is a value I want to pass to another php page and show it.
I have tried
return HttpResponseRedirect('redirected_url.php'.arg)
and it did not work either!
How should I proceed?
Best
You can't use redirect with arg as a second parameter in Django. That parameter determines whether it's a permanent redirect or not. Regardless, the *args option there is for extra data to pass to redirect, not for your variables.
If you're redirecting to another page, you need to pass your variables as a query string. For instance, you could pass a variable name when redirecting in Django this way:
return HttpResponseRedirect('redirected_url.php?name=alex')
And then access it from your PHP script via the superglobal $_GET['name'].

Passing array variable from one page to another in php and joomla

I have an array used in a foreach() loop that I am able to pass from one page to another using a link in the sender page:
$annoncenumber = $book[7];
<a href="index.php?annoncenumber=' . $annoncenumber . '&option=com_aicontactsafe&view=message&layout=message&pf=4&redirect_on_success=">
and in the receiver page:
$annoncenumber = $_GET['annoncenumber'];
This works fine but because of the $annoncenumber in the link I can't use the joomla menu linking system to add modules to the receiver page (menus, footer). I tried to use $_SESSION instead:
$annoncenumber = $book[7];
$_SESSION['lginumero'] = $annoncenumber;
But with this method and using a SEO optimized link:
The session result is incorrect when the person clicks on the link. It only retrieves the last value of the array. So my question is either how to get the joomla modules to show with a link that uses variables OR how to use $_SESSION to correctly pass the array variable to my receiver page when the user clicks the link. Thanks.
You should use the joomla class JSession instead of native PHP $_SESSION, since Joomla sometimes clears the $_SESSION variable.
http://docs.joomla.org/JSession/11.1
You should never use unsanitized $_GET to retrieve the variable, since this is a great security risk. Use the Joomla JRequest::getInt('announcenumber'); instead to properly sanitize the variable.
http://docs.joomla.org/JRequest/11.1
If the PHP file you're using to catch the request is an external file outside Joomla, then you cannot use the core Joomla classes. In that case i suggest that you rewrite your extension to comply with MVC standards.

Categories