[英]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.