Take the value of a php session

Asked

Viewed 1,167 times

1

I have a PHP session that saves the email of the logged in user. Then when this user is registering a text on this page below the text has to be saved with the email field being the one that is logged in, but I do not know how to do it. Could you help me?

    <?php
        require_once ("banco-usuario.php");

        ini_set('display_errors', 1);
        ini_set('display_startup_errors', 1);
        error_reporting(E_ALL);

        session_start();
        include_once "conecta.php";

        if(isset($_POST)){
            $email = $_POST['email'];
            $senha = $_POST['senha'];
            $_SESSION['login'] = $email;

            if(isAluno($conexao,$email,$senha)){

                $_SESSION['tipousuario'] = "aluno";
                header("Location: ../control/home.php");

            }else if(isProfessor($conexao,$email,$senha)){

                $_SESSION['tipousuario'] = "Professor";
                header("Location: ../control/home.php");

            }else{
                header("Location: ../index.php?msg=Usuário ou senha incorretos");
            }
        }
    ?>


    <?php ini_set('display_errors', 1);
      ini_set('display_startup_errors', 1);
      error_reporting(E_ALL); 
    require_once("../cabecalhoInicial.php");
    require_once("banco-usuario.php");

    $titulo = $_POST['titulo'];
    $introducao = $_POST['introducao'];
    $argumento1 = $_POST['argumento1'];
    $argumento2 = $_POST['argumento2'];
    $argumento3 = $_POST['argumento3'];
    $conclusao = $_POST['conclusao'];
$email = $_SESSION['login'];


    if(insereTexto($conexao, $titulo, $introducao, $argumento1, $argumento2, $argumento3, $conclusao, $email)) {?>
      <main>
        <br><br><br><br><br><br>
        <div class="menor">
          <div class="container"><br><br>
            <h5 class="black-text center-align light">O texto <?= $titulo ?> foi cadastrado no sistema VestCollege.</h5>
          </div>
        </div>
      </main>

      <?php }else{
        $msg = mysqli_error($conexao);
        ?>

        <main>
          <br><br><br><br><br><br>
          <div class="menor">
            <div class="container"><br><br>
              <h5 class="black-text center-align light">O texto <?= $titulo ?> não foi cadastrado.</h5>
            </div>
          </div>
        </main>
        <?php } include("../rodapeInicial.php"); ?>

Function inserts Ext:

    function insereTexto($conexao, $titulo, $introducao, $argumento1, $argumento2, $argumento3, $conclusao, $email) {
      $query = "insert into texto (titulo, introducao, argumento1, argumento2, argumento3, conclusao, aluno) values ('{$titulo}', '{$introducao}', '{$argumento1}', '{$argumento2}', '{$argumento3}', '{$conclusao}', {$email})";
      return mysqli_query($conexao, $query);
    }

  • $mailRequest = $_SESSION['email'];

  • Jadson, don’t forget the call session_start() always have to be the first PHP parameter on your page that you want to work with sessions, IE, has to be the first top item before any other.

2 answers

0

<?php 
    session_start();
    ini_set('display_errors', 1);
    ini_set('display_startup_errors', 1);
    error_reporting(E_ALL); 
    require_once("../cabecalhoInicial.php");
    require_once("banco-usuario.php");

    $titulo = $_POST['titulo'];
    $introducao = $_POST['introducao'];
    $argumento1 = $_POST['argumento1'];
    $argumento2 = $_POST['argumento2'];
    $argumento3 = $_POST['argumento3'];
    $conclusao = $_POST['conclusao'];
    $email = $_SESSION['login'];
        if(insereTexto($conexao, $titulo, $introducao, $argumento1, $argumento2, $argumento3, $conclusao, $email)) {?>
          <main>
            <br><br><br><br><br><br>
            <div class="menor">
              <div class="container"><br><br>
                <h5 class="black-text center-align light">O texto <?= $titulo ?> foi cadastrado no sistema VestCollege.</h5>
              </div>
            </div>
          </main>

          <?php }else{
            $msg = mysqli_error($conexao);
            ?>

            <main>
              <br><br><br><br><br><br>
              <div class="menor">
                <div class="container"><br><br>
                  <h5 class="black-text center-align light">O texto <?= $titulo ?> não foi cadastrado.</h5>
                </div>
              </div>
            </main>
            <?php } include("../rodapeInicial.php"); ?>

0

If you just want to take the amount, just do $_SESSION['email'];

  • does not work. explain me better or help me applying this code I sent

  • What mistake are you making? This is how you take the value of a session. Remember to call the session_start() function in the files you are using session. This will cause the session to be created if it does not exist or redeemed.

Browser other questions tagged

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