I am completely new on PHP, I crated two functions stored on a different folder and file. I would like to know how to call these functions on any file. Here my files.
\functions\funciones.php file
<?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
<?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.
You could use 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
ComprobarSesion();
Hope this helps :)