wepnop Posted June 3, 2011 Share Posted June 3, 2011 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... Quote Link to comment https://forums.phpfreaks.com/topic/238298-stable-way-to-execute-some-code-only-at-the-start-of-the-program/ Share on other sites More sharing options...
WebStyles Posted June 3, 2011 Share Posted June 3, 2011 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. Quote Link to comment https://forums.phpfreaks.com/topic/238298-stable-way-to-execute-some-code-only-at-the-start-of-the-program/#findComment-1224576 Share on other sites More sharing options...
wepnop Posted June 3, 2011 Author Share Posted June 3, 2011 Thanks, its working. Quote Link to comment https://forums.phpfreaks.com/topic/238298-stable-way-to-execute-some-code-only-at-the-start-of-the-program/#findComment-1224628 Share on other sites More sharing options...
WebStyles Posted June 3, 2011 Share Posted June 3, 2011 Cool. Glad to have helped. Please mark this post as 'resolved'. Quote Link to comment https://forums.phpfreaks.com/topic/238298-stable-way-to-execute-some-code-only-at-the-start-of-the-program/#findComment-1224644 Share on other sites More sharing options...
wepnop Posted June 5, 2011 Author Share Posted June 5, 2011 Well, actually my problem is that somewhat this still failing, but its too random i think. The problem is that times it dont create the register when its not instantied. Its a wtf but its happening... Quote Link to comment https://forums.phpfreaks.com/topic/238298-stable-way-to-execute-some-code-only-at-the-start-of-the-program/#findComment-1225282 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.