Asked  7 Months ago    Answers:  5   Viewed   42 times

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?

 Answers

89

You can store the variables in the session.

http://www.w3schools.com/php/php_sessions.asp

Saturday, May 29, 2021
 
Owen
answered 7 Months ago
39

You can use either, but the proper statuscode to use for redirect-after-post is 303.

The confusion has a historical explanation. Originally, 302 specified that the browser mustn't change the method of the redirected request. This makes it unfit for redirect-after-post, where you want the browser to issue a GET request. However, all browsers seems to misinterpret the specs and always issue a GET request. In order to clear up the ambiguity HTTP/1.1 specified two new codes: 303 and 307. 303 essentially specifies the de-facto interpretation of 302, while 307 specifies the original specification of 302. So in practice 302 and 303 are interchangeable, and in theory 302 and 307 are.

If you really care about compatibility, 302 is a safer bet than 303, since HTTP/1.0 agents may not understand 303, but all modern browsers speak HTTP/1.1, so it isn't a real issue. I would recommend using 303, since that's the most correct thing to do.

On a side-note; The Location field should be a full URL. In practice it doesn't matter - browsers are forgiving - but if you care about the specs, that's the proper thing to do.

Wednesday, March 31, 2021
 
mistero
answered 9 Months ago
98

It should be something like this.

info.php

class Connection {
   // these two variable should be declared within the class.
   protected $db; // to be able to access these variables from a diff class
   protected $db1; // either their scope should be "protected" or define a getter method.

   public function __construct() {
      $this->connect();
   }

   private function connect() {
       $this->db = 'hello world';
       $this->db1 = 'hi';
   }
}

prd.php

require_once 'info.php';

// you are accessing the Connection class in static scope
// which is not the case here.
class prdinfo extends Connection {
   public function __construct() {
       // initialize the parent class
       // which in turn sets the variables.
       parent::__construct();
   }

   public function productId() {
        echo $this->db;
        echo $this->db1;
   }
}


$prd = new prdinfo ();
$prd->productId ();

This is a basic demonstration. Modify it as per your needs. More here - http://www.php.net/manual/en/language.oop5.php

Saturday, May 29, 2021
 
Trott
answered 7 Months ago
71

Allright, I solved this myself with some more investigation. All I had to do was to add a parameter that sets the HTTP Version to 1.1, as follows:

HttpParams params = new BasicHttpParams();
params.setParameter(CoreProtocolPNames.PROTOCOL_VERSION, HttpVersion.HTTP_1_1);
HttpClient httpclient = new DefaultHttpClient(params);

I found this thanks to the very nice HttpHelper Class from and-bookworm and some trial-and-error.

If I remember correctly, HTTP 1.0 opens a new TCP connection for every request. Does that explain the large delay?

A HTTP POST request now takes between 50 and 150 ms over WLAN and something between 300 and 500 ms over 3G.

Saturday, July 10, 2021
 
tdous
answered 5 Months ago
23

Use one of the variations of AppDomain.CreateDomain that takes an AppDomainSetup argument. In the AppDomainSetup object, set the AppDomainInitializerArguments member to the string array you want passed to the new app domain.

See sample code at http://msdn.microsoft.com/en-us/library/system.appdomainsetup.appdomaininitializerarguments.aspx.

With the code in the question, the change might look like (not tested):

static voide Main(string[] args) {
    _str = "abc";

    AppDomainSetup setup = new AppDomainSetup();
    setup.AppDomainInitializer = new AppDomainInitializer(MyNewAppDomainMethod);
    setup.AppDomainInitializerArguments = new string[] { _str };

    AppDomain domain = AppDomain.CreateDomain(
        "Domain666",
        new Evidence(AppDomain.CurrentDomain.Evidence),
        setup);

    Console.WriteLine("Finished");
    Console.ReadKey();
}

static void MyNewAppDomainMethod(string[] args) {
    ...
}
Friday, October 29, 2021
 
KMC
answered 1 Month ago
KMC
Only authorized users can answer the question. Please sign in first, or register a free account.
Not the answer you're looking for? Browse other questions tagged :
 
Share