简体   繁体   English

将值从 php 传递到另一个 php 文件

[英]pass value from php to another php file

i have a server.php file witch handle the registration form.我有一个 server.php 文件来处理注册表。

The server.php looks like: server.php 看起来像:

<?php
session_start();

// initializing variables
$username = "";
$email    = "";
$ig_name = "";
$errors = array(); 

// connect to the database
$db = mysqli_connect('localhost', 'secret', 'secret', 'test');

// REGISTER USER
if (isset($_POST['reg_user'])) {
  // receive all input values from the form
  $username = mysqli_real_escape_string($db, $_POST['username']);
  $ig_name = mysqli_real_escape_string($db, $_POST['ig_name']);
  $password_1 = mysqli_real_escape_string($db, $_POST['password_1']);
  $password_2 = mysqli_real_escape_string($db, $_POST['password_2']);

  // form validation: ensure that the form is correctly filled ...
  // by adding (array_push()) corresponding error unto $errors array
  if (empty($username)) { array_push($errors, "A Felhasználónév mező nem lehet üres."); }
  if (empty($ig_name)) { array_push($errors, "Az IG név mező nem lehet üres."); }
  if (empty($password_1)) { array_push($errors, "A Jelszó mező nem lehet üres."); }
  if ($password_1 != $password_2) {
    array_push($errors, "A két jelszó nem egyezik.");
  }

  // first check the database to make sure 
  // a user does not already exist with the same username and/or email
  $user_check_query = "SELECT * FROM users WHERE username='$username' OR ig_name='$ig_name' LIMIT 1";
  $result = mysqli_query($db, $user_check_query);
  $user = mysqli_fetch_assoc($result);
  
  if ($user) { // if user exists
    if ($user['username'] === $username) {
      array_push($errors, "A felhasználónév már regiszrálva van.");
    }
  }

  // Finally, register user if there are no errors in the form
  if (count($errors) == 0) {
    $password = md5($password_1);//encrypt the password before saving in the database

    $query = "INSERT INTO users (username, ig_name, password) 
              VALUES('$username', '$ig_name', '$password')";
    mysqli_query($db, $query);
    $_SESSION['username'] = $username;
    $_SESSION['success'] = "Sikeresen bejelentkeztél.";
    $_SESSION['ig_name'] = $user['ig_name'];
    header('location: index.php');
  }
}
if (isset($_POST['login_user'])) {
    $username = mysqli_real_escape_string($db, $_POST['username']);
    $password = mysqli_real_escape_string($db, $_POST['password']);
    $ig_name = mysqli_real_escape_string($db, $_POST['ig_name']);
  
    if (empty($username)) {
        array_push($errors, "A Felhasználó mező nem lehet üres");
    }
    if (empty($password)) {
        array_push($errors, "A Jelszó mező nem lehet üres.");
    }
  
    if (count($errors) == 0) {
        $password = md5($password);
        $query = "SELECT * FROM users WHERE username='$username' AND password='$password'";
        $results = mysqli_query($db, $query);
        if (mysqli_num_rows($results) == 1) {
          $_SESSION['username'] = $username;
          $_SESSION['success'] = "Sikeresen bejelentkeztél.";
          header('location: index.php');
        }else {
            array_push($errors, "Hibás adatot adtál meg.");
        }
    }
  }
  
  ?>

i wanna get the $ig_name variable as value from SQL the the rows name is "ig_name" then use it in settings.php我想从 SQL 中获取 $ig_name 变量作为值,行名称是“ig_name”,然后在设置中使用它。php

my settings.php looks like:我的 settings.php 看起来像:

 <?php 
  session_start(); 

  if (!isset($_SESSION['username'])) {
    $_SESSION['msg'] = "You must log in first";
    header('location: login.php');
  }
  if (isset($_GET['logout'])) {
    session_destroy();
    unset($_SESSION['username']);
    header("location: login.php");
  }
?>
<!DOCTYPE html>
<html>
<head>
    <title>CDS - Adatbázis</title>
    <link rel="stylesheet" type="text/css" href="style.css">   
</head>
<body>
<span style="margin-left:30px;top:10px;position:relative;font-size:30px;cursor:pointer;color:white;" onclick="openNav()">&#9776; open</span>
<div id="mySidenav" class="sidenav">
  <a href="javascript:void(0)" class="closebtn" onclick="closeNav()">&times;</a>
  <a href="index.php">Főoldal</a>
  <div w3-include-html="content.html"></div>
  <a href="#">Keresett személy / Jármű</a>
  <a href="#">Ismert rendszámok</a>
  <a href="#">Beállítások</a>
  <a href="index.php?logout='1'">Kijelentkezés</a>
</div>
<script>
function openNav() {
  document.getElementById("mySidenav").style.width = "250px";
}

function closeNav() {
  document.getElementById("mySidenav").style.width = "0";
}

$(function(){
      $("#includedContent").load("b.html"); 
    });
</script>

<div class="header">
    <h2>Beállítások</h2>
</div>
<div class="content">
    <!-- notification message -->
    <?php if (isset($_SESSION['success'])) : ?>
      <div class="error success" >
        <h3>
          <?php 
            echo $_SESSION['success']; 
            unset($_SESSION['success']);
          ?>
        </h3>
      </div>
    <?php endif ?>

    <!-- logged in user information -->
    <?php  if (isset($_SESSION['username'])) : ?>
        <?php include('server.php') ?>
          
        <div>
            <table style="width: 60%;" border="0" cellpadding="5">
            <tbody>
                <tr>
                    <td>&nbsp;Felhasználónév:</td>
                    <td>&nbsp;<strong><?php echo $_SESSION['username']; ?></strong></td>
                </tr>
                <tr>
                    <td>&nbsp;IG neved:</td>
                    <td>&nbsp;<strong><?php $ig_name =$_GET['ig_name']; echo $ig_name; ?></strong></td>
                    <td>&nbsp;</td>
                </tr>
                <tr>
                    <td>&nbsp;Jelszó:</td>
                    <td>&nbsp;</td>
                    <td>&nbsp;</td>
                </tr>
            </tbody>
            </table>
        </div>
    <?php endif ?>
</div>
</body>
</html> 

Some html string is hungarian, so dont care of it.有些 html 字符串是匈牙利语,所以不要在意。

How should i get, then send the "ig_name" rows in settings?我应该如何获取,然后在设置中发送“ig_name”行? i dont wanna use a new sql connection, just in server.php我不想在 server.php 中使用新的 sql 连接

You have already made a session of $ig_name in your server.php on this line $_SESSION['ig_name'] = $user['ig_name'];您已经在服务器中创建了 $ig_name 的 session。php 在这一行 $_SESSION['ig_name'] = $user['ig_name']; so you only need to replace所以你只需要更换

<?php $ig_name =$_GET['ig_name']; echo $ig_name; ?>

with

<?php echo $_SESSION['ig_name']; ?>

in your settings.php在您的设置中。php

Include your server.php file at the top of settings.php file在 settings.php 文件的顶部包含您的服务器。php 文件

include "server.php"包括“server.php”

now you can access the "ig_name" variable现在您可以访问“ig_name”变量

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM