Jump to content

Stable way to execute some code only at the start of the program


wepnop

Recommended Posts

I need a clean and stable way to execute code only when the program starts. I use that to create a register in the $_SESSION,  and do some other things that i really need one time. I also save a error array in my register object so if its recreated each time that is losed.

 

This is the file that is called always in all my files, in the first line.

 

<?php

# Various includes for functions
include 'bd.php';
include 'sesiones.php';
include 'test.php';
include 'web_inventario.php';
include 'errores.php';
include 'usuarios.php';
include_once 'registroglobal.php';

# This start the session so i can check for it and store in it without warnings and problems.
session_start();

# This code is the code that will be executed the first time. reg is the register thats persistent through sesions. The idea is that
# if the register isnt isset it will be settled and this is all, just for the first time-.
if (!isset($_SESSION['reg'])) {
$reg = RegistroGlobal::GetInstance();
p('SETEJA SES');
# Configura para usar la libreria basica 
$reg->setConf('Libreria usada', 'Inventario');

        
include '/../ijb_categorias.php';	
include '/../ijb_configuracion.php';	

        # Set various configurations and save in $_SESSION
$reg->setInvConfig($configuracion);
$reg->setInvCategorias($categorias);


$_SESSION['reg'] = $reg;

$_SESSION['reg']->setConf('Ruta BD', $configuracion['conexion_bd'][0]); 

$_SESSION['reg']->setConf('Nombre usuario', $configuracion['conexion_bd'][1]);
$_SESSION['reg']->setConf('Contraseña', $configuracion['conexion_bd'][2] ); 
$_SESSION['reg']->setConf('Nombre BD', $configuracion['conexion_bd'][3]);
$_SESSION['reg']->setConf('Logs', $configuracion['Logs'][3]);
# Primero setea la conexion de la base de datos sin selccionar ninguna,
# de esta forma no da error y permite crear una nueva o navegar sin ella
$_SESSION['reg']->setDB(conectar_bd(false));
if ($configuracion['Recargar inicio'] ) {
	cargarConfiguracionCategorias();

	#echo 'MARCA REGGGGGGGGGGGGGGGGGGGGGGGGGGGG<br><br>';
}
# Actualiza el administrador
$usradm = $configuracion['Administrador'];
$cons = "UPDATE `usuarios` SET rango = 3 WHERE nombre = '$usradm'";
modificar_bd($cons);


}

# Code to execute always(conect to database)
# Se asegura de conectar con la base de datos seleccionando, podria fer consulta sql de si existe o no bd
$_SESSION['reg']->setDB(conectar_bd());




?>

 

But this isnt working. Its giving me troubles. When i think it works, in other computer or at the next day it gets broken. I dont understand why i only need that...

Link to comment
Share on other sites

I'm not really sure what you want. From what I understand, you have a set of functions you want to run only once. Why don't you use a session var to control if it has been run or not?

 

something like:

$_SESSION['initial_setup'] = 'done';

 

and only execute the code if (!isset($_SESSION['initial_setup']) || $_SESSION['initial_setup'] != 'done') { ..}

 

again, I'm not sure if this is what you intended.

 

hope it helps.

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.