prime Posted August 31, 2007 Share Posted August 31, 2007 I am including a php script I've written for every page. I don't need to include it here as its just a script to basically set site modes for maintenance and passwords and such, no big deal. but I seem to have run into a very very simple problem, which for the life of me I cant seem to work out. I want to have an "or die" function on an include like thus: <?php $local_pass = "off"; //not active yet $local_mode = "open"; @include("mscript.php") or die("unable to access MScript"); ?> But for some weird reason I cant seem to get this or die working on an include, is there something real basic here I'm missing? Quote Link to comment Share on other sites More sharing options...
phpSensei Posted August 31, 2007 Share Posted August 31, 2007 You need an if/else condition... Quote Link to comment Share on other sites More sharing options...
prime Posted August 31, 2007 Author Share Posted August 31, 2007 I basically just want the page to die though if it cant access that file. And I'm not sure how to check if its accessible via an include Quote Link to comment Share on other sites More sharing options...
Ken2k7 Posted August 31, 2007 Share Posted August 31, 2007 What do you mean if it can't access that file? It won't mess up a code. Quote Link to comment Share on other sites More sharing options...
prime Posted August 31, 2007 Author Share Posted August 31, 2007 I know. But I am running it on a page that I don't want displayed at all if it cant include the file Quote Link to comment Share on other sites More sharing options...
Jessica Posted August 31, 2007 Share Posted August 31, 2007 use require instead of include Quote Link to comment Share on other sites More sharing options...
TutorMe Posted August 31, 2007 Share Posted August 31, 2007 The folloing code is completely untested, and coming from a noob, but try something like: <?php $success = @include("mscript.php"); if (!$success) { die('unable to access MScript') } $local_pass = "off"; //not active yet $local_mode = "open"; @include("mscript.php") ?> Quote Link to comment Share on other sites More sharing options...
phpSensei Posted August 31, 2007 Share Posted August 31, 2007 The folloing code is completely untested, and coming from a noob, but try something like: <?php $success = @include("mscript.php"); if (!$success) { die('unable to access MScript') } $local_pass = "off"; //not active yet $local_mode = "open"; @include("mscript.php") ?> there you see, if statement. Quote Link to comment Share on other sites More sharing options...
Fadion Posted August 31, 2007 Share Posted August 31, 2007 $success = @include("mscript.php") or die('Unable to access MScript'); //code Quote Link to comment Share on other sites More sharing options...
prime Posted August 31, 2007 Author Share Posted August 31, 2007 None of thats working still :-( Quote Link to comment Share on other sites More sharing options...
darkfreaks Posted August 31, 2007 Share Posted August 31, 2007 <?php $success = @require("mscript.php"); if (!$success) { die('unable to access MScript') } $local_pass = "off"; //not active yet $local_mode = "open"; @require("mscript.php") ?> Quote Link to comment Share on other sites More sharing options...
Jessica Posted August 31, 2007 Share Posted August 31, 2007 Uhm...require? Quote Link to comment Share on other sites More sharing options...
prime Posted August 31, 2007 Author Share Posted August 31, 2007 a straight require works good and gives a blank screen if it doesn't work. But I am also after a gracefull degrade message and that if statment just isn't working it's giving: Parse error: syntax error, unexpected '}' in /home/content/m/e/t/metaconcert/html/primefalcon/index.php on line 12 Quote Link to comment Share on other sites More sharing options...
Fadion Posted August 31, 2007 Share Posted August 31, 2007 ok try echoing a succesfully included file: echo include 'myfile.php'; //ex it echoes '1' then: if((include 'myfile.php') != '1'){ die('Didnt load'); } //code Quote Link to comment Share on other sites More sharing options...
darkfreaks Posted August 31, 2007 Share Posted August 31, 2007 what is line 12? Quote Link to comment Share on other sites More sharing options...
Jessica Posted August 31, 2007 Share Posted August 31, 2007 require('file.php') OR die("error message"); Quote Link to comment Share on other sites More sharing options...
teng84 Posted August 31, 2007 Share Posted August 31, 2007 use require or include depends on your needs any way try to use require_once and include_once try http://www.php.net/manual/en/function.file-exists.php Quote Link to comment Share on other sites More sharing options...
teng84 Posted August 31, 2007 Share Posted August 31, 2007 require('file.php') OR die("error message"); oops i dont this would work if you cant find the path and u use require this will terminate the whole script but for include it will continue but still error will occur i vote!! http://www.php.net/manual/en/function.file-exists.php Quote Link to comment Share on other sites More sharing options...
prime Posted August 31, 2007 Author Share Posted August 31, 2007 <?php $local_pass = "off"; //not active yet $local_mode = "open"; require("mscript.php") or die("unable to access script"); ?> Just gives a blank page if it fails, it wont give the die message. and with the Parse error: syntax error, unexpected '}' in /home/content/m/e/t/metaconcert/html/primefalcon/index.php on line 12 <?php $success = @require("mscript.php"); if (!$success) { die('unable to access MScript') }//This is line 12 $local_pass = "off"; //not active yet $local_mode = "open"; @require("mscript.php") ?> Quote Link to comment Share on other sites More sharing options...
teng84 Posted August 31, 2007 Share Posted August 31, 2007 use require or include depends on your needs any way try to use require_once and include_once try http://www.php.net/manual/en/function.file-exists.php Quote Link to comment Share on other sites More sharing options...
darkfreaks Posted August 31, 2007 Share Posted August 31, 2007 instead of include or require try require_once or include_once Try this was missing a ; at the end of the if statement <?php $success = @require("mscript.php"); if (!$success){ die('unable to access MScript'); } $local_pass = "off"; //not active yet $local_mode = "open"; @require("mscript.php"); ?> Quote Link to comment Share on other sites More sharing options...
teng84 Posted August 31, 2007 Share Posted August 31, 2007 you dont have a terminator $aray=array('page.php', 'page2.php'); foreach($aray as $array) if (file_exists($array)) { require_once $array; } else { echo "The file $array does not exist"; } try i havent test that but that will test all your file you want to include by adding the value in an array Quote Link to comment Share on other sites More sharing options...
prime Posted August 31, 2007 Author Share Posted August 31, 2007 The require was still giving blank, so I changed to include again so it reads <?php $success = @include("mscript.php"); if (!$success){ die('unable to access MScript'); } $local_pass = "off"; //not active yet $local_mode = "open"; @include("mscript.php"); ?> and that works perfectly, thank you so much, so much for the simple include or die I thought it would be lol Quote Link to comment Share on other sites More sharing options...
teng84 Posted August 31, 2007 Share Posted August 31, 2007 instead of include or require try require_once or include_once Try this was missing a ; at the end of the if statement <?php $success = @require("mscript.php"); if (!$success){ die('unable to access MScript'); } $local_pass = "off"; //not active yet $local_mode = "open"; @require("mscript.php"); ?> that wont work thats the same as jesirose did look when we assign anything in a variable the action is also happening like $x=include 'fdsf.php'; even if you do that still the script or php will read your code as including that file so if the code cant find it it will give an error note: the result for that code you produce is not boolean so error will occur Quote Link to comment Share on other sites More sharing options...
darkfreaks Posted August 31, 2007 Share Posted August 31, 2007 no i was still right he was just missing 2 terminators in his code so he got errors Quote Link to comment 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.