简体   繁体   English

如何调用存储在不同文件夹和文件中的函数?

[英]How call a function that is stored on a different folder and file?

I am completely new on PHP, I crated two functions stored on a different folder and file. 我对PHP完全陌生,我创建了两个存储在不同文件夹和文件中的函数。 I would like to know how to call these functions on any file. 我想知道如何在任何文件上调用这些函数。 Here my files. 这是我的文件。

\\functions\\funciones.php file \\ functions \\ funciones.php文件

    <?php 
    //Comprueba si existe una sesión activa, si no redirecciona al index.php
    function ComprobarSesion () {
        if (isset($_SESSION['usuario'])){
        header('Location: index.php');
        }
    }
    // Funcion conexión a la DB. Validar si tambien se peude meter en una funcion. 
    function Conexion_DB(){
        try {
            $conexion = new PDO('mysql:host=127.0.0.1;dbname=login','root','');
        } catch (PDOException $e) {
            echo "Error: ".$e->getMessage();
        }
    }
    ?>

\\index.php file \\ index.php文件

    <?php session_start();
    if (isset($_SESSION['USUARIO'])) {
        header('Location: contenido.php');
    }   else {
        header('Location: registro.php');
    }
    ?>

As you can see in the "index.php" file, I have the code that I want to remove and put the function... but I don't know how to replace it. 如您在“ index.php”文件中看到的,我有要删除并放入该函数的代码...但是我不知道如何替换它。

You could use require_once. 您可以使用require_once。

Put this at the top of your index-file 将其放在索引文件的顶部

<?php 
require_once('/absolute/path/to/your/file.php');

Replace the path and desired file. 替换路径和所需的文件。

Then you can call the function in your index.php like this 然后您可以像这样在index.php中调用该函数

ComprobarSesion();


Hope this helps :) 希望这可以帮助 :)

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

相关问题 如何从同一服务器的不同用户文件夹中调用文件? - How to call file from different user folder in same server? PHP-调用未定义函数,该函数需要来自其他文件夹的文件并使用名称空间 - PHP - Call to undefined function requiring a file from a different folder and using namespace 如何打开存储在 laravel 的 stoarge 文件夹中的文件? - How to open file stored in stoarge folder in laravel? PHP-从存储在其他文件夹中的文件路径获取文件的URL - PHP - Get URL of a file from its path stored in different folder 如何调用位于不同文件中的函数来设置$ _SESSION变量 - How to call a function that is located in a different file to set $_SESSION variables 如何从不同的php文件调用javascript函数 - How to call a javascript function from a different php file 如何从存储在变量中的字符串调用 function? - How to call a function from a string stored in a variable? 如何调用存储在字符串变量中的函数? [PHP] - How to call function stored in string variable? [PHP] 如何使用doctrine调用PostgreSQL存储函数 - How to call a PostgreSQL stored function with doctrine 如何调用PHP中存储在Class属性中的function - How to call function stored in Class property in PHP
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM