How to pass data from one php to another without form?

Asked

Viewed 2,961 times

-3

A very simple question, though, I don’t know or remember. How can I send data from one php file to another, tb in php? Not the case of form.

php 1:

header('Content-Type: text/html; charset=utf-8');
header('Access-Control-Allow-Origin: *');
header('Access-Control-Allow-Methods: GET, POST, OPTIONS');
header('Access-Control-Allow-Headers: Content-Type,X-Prototype-Version,X Requested-With');

include_once("conPDO.php");

$pdo = conectar();

$msg = utf8_decode($_GET['mensagem']);
$idUsuario  = $_GET['idUsuario'];
$idCep  = $_GET['idCep'];
$nomeRemetente  = $_GET['nome'];
$usuarioRemetente  = $_GET['usuario'];
$uf = $_GET['uf'];
$cidade = $_GET['cidade'];
$bairro = $_GET['bairro'];
$logradouro = $_GET['logradouro'];
$dia  = $_GET['dia'];
$hora  = $_GET['hora'];
$foto = '';

$cidade = utf8_decode($cidade);
$bairro = utf8_decode($bairro);
$logradouro = utf8_decode($logradouro);

header('Location: websocket/src/MyApp/Chat.php?msg='.$msg);

I want to pass these variables, above, to the following php below

php 2:

<?php
$msg = $_GET['msg'];
echo $msg;

namespace MyApp;
use Ratchet\MessageComponentInterface;
use Ratchet\ConnectionInterface;

class Chat implements MessageComponentInterface {
    protected $clients;

    public function __construct() {
        $this->clients = new \SplObjectStorage;
    }

    public function onOpen(ConnectionInterface $conn) {
        // Store the new connection to send messages to later
        $this->clients->attach($conn);

        echo "New connection! ({$conn->resourceId})\n";
    }

    public function onMessage(ConnectionInterface $from, $msg) {
        $numRecv = count($this->clients) - 1;
        echo sprintf('Connection %d sending message "%s" to %d other connection%s' . "\n"
            , $from->resourceId, $msg, $numRecv, $numRecv == 1 ? '' : 's');
        $dados = json_decode($msg, true);
        $dados['id'] = $from->resourceId;


        foreach ($this->clients as $client) {

                // The sender is not the receiver, send to each client connected
                $client->send(json_encode($dados));

        }
    }

  • point to the specific location, in the second file, where the variables would be read.. There are several ways to solve from a gross use of global to object orientation..

  • Sorry, but what do you mean, point out the specific location in the second file? And it wouldn’t be in the first?

  • show where the variables of the first file are read in the second file... make a drawing, put a comment in the code, finally

  • @Danielomine, I can not draw here kkk but put in bold, give a reread.

  • 1

    sorry.. unavailable to reply... need to collaborate if you want some help right, because it takes other people’s time...

  • Buddy, I’ve put in bold what I need, it doesn’t help?

  • You have a PHP page that receives the data and you want to pass it on to another PHP page? Why not process the data on the page that receives it?

  • Why this other page is an API that’s ready and I got from git, understand @Fleuquerlima?

Show 3 more comments

2 answers

3


try this on arquivo2.php:

include 'Arquivo1.php';

public function Alguma_Função() {
    global $msg;
}

or

include 'Arquivo1.php';
private $newmsg = '';

public function setMSG() {
    global $msg;
    $this->newmsg = $msg;
}
  • 1

    Solved the problem of the author of the question, but I think it is important to make it clear to all readers that in this case there are no "variables being passed from one PHP to another" as requested in the question, and yes file 2 is being executed with an internal copy of the code 1. NOTE: I’m just clearing that up so I don’t mislead other readers. Nor is it the fault of the answer, but as the questioner does not have the basic knowledge of PHP for complexity codes posted in the question and accepted the answer that solves otherwise, one might find that the answer actually passes parameters.

  • @Bacco goes from interpreting what it means to "move from one file to the other", in fact; I think, that this is not the best way.

  • Again, it is not a criticism of the answer (after all, it generated the "final effect" that the author wanted). Just a warning to those who are learning and tomorrow or later read the question and answer, understand that it is a solution that solves (well solved) but for a technical reason other than described in the question.

2

It is very difficult to understand your question. But some alternatives:

Redirect from archiv11 to the archiv2 passing the data via GET (roughly):

Your URL would be http://youdomain.com.br?variavel1=XXX&variavel2=YYY&variavel3=ZZZZ

And in the archiv2 you recover using the $_GET: For example:

$variavel1 = $_GET['variavel1'];

Another way is you store this in a global variable, include the archiv11 in the archiv2 and retrieve her.

Using Sessions

In the archiv11

session_start();
$_SESSION['msg'] = $_GET['msg'];
header('Location: arquivo2.php');

In the archiv2

session_start();
$msg = $_SESSION['msg'];
  • Seriously, is my question so complicated to understand? I think it’s so obvious that you’re not getting kkkk. I want the variables: $msg, $idUsuario, $idCep, $userRemetent, $userRemetent, $Uf, $city, $neighborhood, $street, $day, $hour, $photo, automatically transferred from one php file to another php file.

  • To use with GET, I would have to pass via url, and a user would have to click a button for it and not want to. It should be something like a header('Location:....');

  • Yes, the URL you can access as you like... either with the action of a button or with a header('Location'). If you want to automate, use the header by passing the values via get

  • Yes, I tried this: header('Location: websocket/src/Myapp/Chat.php?msg='.$msg); but it didn’t work. And this: $msg = $_GET['msg']; echo $msg; in the other file.

  • I edited the post so you can see how the files are now.

  • I put an alternative in the answer with session usage. I think it will solve your problem. In archive2, after using Session you can destroy it

  • 1

    give a include in file 2 with file 1 and use the global $msg

  • Thus closed: include_once('websocket/src/Myapp/Chat.php'); global $msg;

Show 3 more comments

Browser other questions tagged

You are not signed in. Login or sign up in order to post.