Hi guys I'm trying to do a strange thing, I want put all of my code, of all my website pages inside an if, for example I want do this:
<?php
include('..../...../php/secure-open.php');
if(......){
ALL OF WEBSITE CODE HERE
include('..../...../php/secure-close.php');
}
Where secure-open.php is:
<?php
if(security check here){
?>
And secure-close.php is
<?php
}else{
die();
}
?>
So all the will will be executed only if the security condition is true, but when I try to do it, it says, Unexpected document ends on line`....
Looks like you have missed closing bracket :)
You really should not resolve your security checks that way, but if you really sure that's what you need, move "ALL OF WEBSITE CODE" to different file and just include it inside that if.
if(security_check) {
include "website.php";
} else {
die();
}
And one more easy-to-implement fast check:
index.php:
if (security_check) {
define('SECURE', true);
}
require "website.php";
website.php:
if (!defined('SECURE')) {
die();
}
Related
else header not work in login function
if($result=mysqli_query($conn,$sql)){
while($row=mysqli_fetch_row($result)); {
if(!empty($row)) {
header('Location:welcome.html');
}
else {
header('Location:password_wrong.html');
}
}
while($row=mysqli_fetch_row($result)); {
to
while($row=mysqli_fetch_row($result)){
no
;
This is how the computer understands your code:
if($result=mysqli_query($conn,$sql)){
while($row=mysqli_fetch_row($result)) {
}
{
if(!empty($row)) {
header('Location:welcome.html');
} else {
header('Location:password_wrong.html');
}
}
There is a missing closing brace at the end (or somewhere else - the position is very important)
Fixing the indentation often help understanding such problems
The empty() after the while loop works AFAIK, but it's very unusual. Rethink your code and verify it's really what you wanted.
if the while loop line doesn't have a closing brace and no semicolon, the if condition goes inside the loop. If this is what you wanted: the empty() function will always return false (= !empty() is always true)
I assume your query returns rows if the login was successful, and nothing if the login failed.
Try use exit(); after each header('Location:...);
The header()-function doesn't stop the script by itself, so the script below the if/else will still get executed. The exit()-function makes sure that the scripts stops after the header()-function.
I've got my login and session validity functions all set up and running.
What I would like to do is include this file at the beginning of every page and based on the output of this file it would either present the desired information or, if the user is not logged in simply show the login form (which is an include).
How would I go about doing this? I wouldn't mind using an IF statement to test the output of the include but I've no idea how to go about getting this input.
Currently the login/session functions return true or false based on what happens.
Thanks.
EDIT: This is some of the code used in my login/session check but I would like my main file to basically know if the included file (the code below) has returned true of false.
if ($req_method == "POST"){
$uName = mysql_real_escape_string($_POST['uName']);
$pWD = mysql_real_escape_string($_POST['pWD']);
if (login($uName, $pWD, $db) == true){
echo "true"; //Login Sucessful
return true;
} else {
echo "false";
return false;
}
} else {
if (session_check($db) == true){
return true;
} else {
return false;
}
}
You could mean
if (include 'session_check.php') { echo "yeah it included ok"; }
or
logincheck.php'
if (some condition) $session_check=true;
else $session_check=false;
someotherpage.php
include 'session_check.php';
if ($session_check) { echo "yes it's true"; }
OR you could be expecting logincheck.php to run and echo "true" in which case you're doing it wrong.
EDIT:
Yes it was the latter. You can't return something from an included file, it's procedure not a function. Do this instead and see above
if (session_check($db) == true){
$session_check=true;
} else {
$session_check=false;
}
Actually..
$session_check=session_check($db);
is enough
Depending on where you want to check this, you may need to declare global $session_check; or you could set a constant instead.
you could have an included file which sets a variable:
<?php
$allOk = true;
and check for it in you main file:
<?php
include "included.php";
if ($allOk) {
echo "go on";
} else {
echo "There's an issue";
}
Your question seems to display some confusion about how php includes work, so I'm going to explain them a little and I think that'll solve your problem.
When you include something in PHP, it is exactly like running the code on that page without an include, just like if you copied and pasted. So you can do this:
includeme.php
$hello = 'world';
main.php
include 'includeme.php';
print $hello;
and that will print 'world'.
Unlike other languages, there is also no restriction about where an include file is placed in PHP. So you can do this too:
if ($whatever = true) {
include 'includeme.php';
}
Now both of these are considered 'bad code'. The first because you are using the global scope to pass information around and the second because you are running globally scoped stuff in an include on purpose.
For 'good' code, all included files should be classes and you should create a new instance of that class and do stuff, but that is a different discussion.
This question already has answers here:
Is it possible to include an html file inside a php file
(3 answers)
Closed 9 years ago.
At the moment I have a file like this
<?php
if(some condition)
{
//Dont allow access
}
else
{
echo "<html>My HTML Code</html>";
}
?>
But I wanted to do something like this to keep my php file short and clean.
<?php
if(some condition)
{
//Dont allow access
}
else
{
//print the code from ..html/myFile.html
}
?>
How can I achieve this?
save your html content as seperate template and simply include it
<?php
if(some condition)
{
//Dont allow access
}
else
{
include ("your_file.html");
}
?>
OR
<?php
if(some condition)
{
//Dont allow access
}
else
{
readfile("your_file.html");
}
?>
readfile is faster and less memory intensive than file_get_contents
you may have a look at PHP Simple HTML DOM Parser, seems a good idea for your needs! Example:
// Create a DOM object from a string
$html = str_get_html('<html><body>Hello!</body></html>');
// Create a DOM object from a URL
$html = file_get_html('http://www.google.com/');
// Create a DOM object from a HTML file
$html = file_get_html('test.htm');
Use this code
if(some condition)
{
//Dont allow access
}
else
{
echo file_get_contents("your_file.html");
}
OR
if(some condition)
{
//Dont allow access
}
else
{
require_once("your_file.html");
}
Extending nauphal's answer for a more robust solution..
<?php
if(some condition)
{
//Dont allow access
}
else
{
if(file_exists("your_file.html"))
{
include "your_file.html";
}
else
{
echo 'Opps! File not found. Please check the path again';
}
}
?>
Use functions like
include()
include_once()
require()
require_once()
file_get_contents()
<?php
if(some condition)
{
//Dont allow access
}
else
{
echo file_get_contents("your_file.html");
}
?>
This should do the trick
Or, as nauphal's answer say, simply use include()
Don't forget that, if file doesn't exists, you could have some trouble (so, maybe, check before include or getting content)
I think you want to include your HTML file or have I misunderstood the question.
<?php
if(some condition)
{
//Dont allow access
}
else
{
include ("..html/myFile.html");
}
?>
Way 1:
ob_start();
include "yourfile.html";
$return = ob_get_contents();
ob_clean();
echo $return;
Way 2:
Use templaters, like CTPP, Smarty, etc...
Templaters are useful to transfer some logic from php to template, for example, in CTPP:
$Templater -> params('ok' => true);
$Template -> output('template.html');
in template html:
<TMPL_if (ok) >
ok is true
<TMPL_else>
ok not true
</TMPL_if>
The same ideas are in other templaters.
Templaters are better, cause it helps you to standartize your templates and send all primitive logic to them.
is there any any other command we can use as an alternative to exit(); in php.
Because it breaks my html code at the end of the page if the condition is not met and when script has to exit.
Or if anyone has any other idea to resolve this issue???
Thanks
Update:
html code...
<?php
if username is not in correct format
echo "Please check your username";
exit();
if Username and Password didn't match
echo "Wrong Username or Password.";
exit();
if some other condition not met
echo "Condition not met";
exit();
?>
html code continues...
Now the problem is if any of the condition is not met and the script has to exit, the html code below it, which is a whole webpage, does not display...
And please...I am not a computer geek, had a problem so asked it, but why people vote down the question??? don't understand....
You should probably wrap your code into an if statement:
<?php
if($code == 'ok'){
echo 'ok';
} else {
echo 'not ok';
}
?>
your script doesn't have to exit(), you can add statements where you want and how you want.
As the name suggests, the PHP exit() statement will cause your PHP script to exit, right there and then, and not do anything else. If you want it to carry on processing the rest of the code, just don't use exit().
Looking at your code, what you seem to be aiming for is displaying errors to the user, and then (I would guess) re-showing the form they filled in incorrectly.
Rather than just echoing the errors as soon as you discover them, why not store them into a variable, which can then be displayed at an appropriate point in the HTML? Even the most basic of scripts can benefit from a bit of basic code structure.
As an example (and I stress this is not the One True Pattern for this kind of thing), you could arrange your file something like this:
if ( /* form has been submitted */ )
{
$errors = validate_form();
if ( count($errors) > 0 )
{
display_form($errors);
}
else
{
display_success_message();
}
}
else
{
display_form();
}
function validate_form()
{
$errors = array();
// Series of if conditions, each adding a message to $errors if appropriate
return $errors;
}
function display_form($errors=array())
{
// HTML <ul> list displaying the contents of $errors, if any
// HTML for form
}
function display_success_message()
{
// HTML thanking user for a successful form submission
}
I'm trying to implement caching for a PHP script I'm writing, but I keep running into the following problem. I want the script to be included in other PHP pages, but when I try to pass the cached file and exit the embedded script it exits both the script and the parent page, but doesn't parse the rest of the code on the parent page. See the code below for an example.
index.php
<?php
echo "Hello World!<br />";
include("file2.php");
echo "This line will not be printed";
?>
file2.php
<?php
$whatever = true;
if ($whatever == true) {
echo "file2.php has been included<br />";
exit; // This stops both scripts from further execution
}
// Additional code here
?>
If the above index.php is executed you get the following output:
Hello World!
file2.php has been included
However, I'm trying to get it to look like this:
Hello World!
file2.php has been included
This line will not be printed
Use return; instead of exit; in the included file - this will only halt execution of that script.
Note that you an also use this to return a value to the parent script e.g.
file1.php
<?php
echo 'parent script';
$val = include('file2.php'); //$val will equal 'value'
echo 'This will be printed';
file2.php
<?php
echo 'child script';
return 'value';
Just wrap the "additional code here" in an else statement?
<?php
$whatever = true;
if ($whatever == true) {
echo "file2.php has been included<br />";
} else {
// Additional code here
}
?>
Otherwise I'm not sure what you're getting at. The exit command always terminates the current execution in whole - not just execution of the current file (for which, there is no command)
EDIT
Thanks to comments and posts by PHLAK, tomhaigh, MichaelM, and Mario, I myself learned something today - that you CAN indeed terminate the execution of a single included file w/the return command. Thanks, guys!
I personally try to avoid if-else conditions where possible and use (not sure if there's a coined term for it but) early-exit intercepting conditions.
index.php
<?php
echo 'header';
include 'content.php';
echo 'footer';
?>
content.php
<?php
if ($cached)
{
echo cached_version();
return; // return is not just for functions, in php...
}
//proceed with echoing whatever you want to echo if there's no cached version.
...
...
?>
Why not encapsulate the contents of file2.php into a function. That way you can return from the function when you need to, and the rest of the execution will not halt. eg:
file2.php
<?php
// this function contains the same code that was originally in file2.php
function exe()
{
$whatever = true;
if ($whatever)
{
echo "file2.php has been included <br />";
// instead of exit, we just return from the function
return;
}
}
// we call the function automatically when the file is included
exe();
?>
Leave index.php exactly as it is and you should see the output you are trying to achieve.