-2

How come php in include is executed before html? Because user input is from html. Shouldn't included script be at last?

<?php
include('login.php'); // Includes Login Script

if(isset($_SESSION['login_user'])){
header("location: profile.php");
}
?>
<!DOCTYPE html>
<html>
<head>
<title>Login Form in PHP with Session</title>
<link href="style.css" rel="stylesheet" type="text/css">
</head>
<body>
<div id="main">
<h1>PHP Login Session Example</h1>
<div id="login">
<h2>Login Form</h2>
<form action="" method="post">
<label>UserName :</label>
<input id="name" name="username" placeholder="username" type="text">
<label>Password :</label>
<input id="password" name="password" placeholder="**********" type="password">
<input name="submit" type="submit" value=" Login ">
<span><?php echo $error; ?></span>
</form>
</div>
</div>
</body>
</html>
Vadim Kotov
  • 8,084
  • 8
  • 48
  • 62
webionDev
  • 1
  • 6
  • 1
    I think php read from top to bottom – Masivuye Cokile Jul 06 '17 at 11:41
  • With include you principally Copy the content of your file and paste it right at the place where you've done the include. If you want it to be executed after your html, simply include your login.php at the end of your file. Btw, I think you may should have a look at things like `if(isset($_POST["username"]))` - because you woudln't have any problem if your PHP would be written good, what I don't think it is, since it seems that some part of your PHP cod executed without the form being filled. – Twinfriends Jul 06 '17 at 11:41
  • 3
    Nope.... PHP code is executed from top to bottom (like in most programming languages) except for calls to functions, and loops which are all about controlling the execution flow of the code..... it doesn't know or care about user input being executed first, isn't even aware that the html is used for user input – Mark Baker Jul 06 '17 at 11:41
  • @Twinfriends [`include`](http://php.net/manual/en/function.include.php) **is not** copy-paste. Not even close. – axiac Oct 25 '17 at 15:31
  • @axiac Well then please tell me what is it :) I'm always ready to learn new things, but by simply saying "not even close" I can't learn anything, OP won't learn anything and you don't have any proove that I'm worng. So please tell me how it works, and I'll be thankfull, otherwise simply don't comment ;) I'm quite sure there are many scenarios where include behavs ike a copy-paste. – Twinfriends Oct 26 '17 at 07:28
  • @Twinfriends well, maybe I exaggerated a little (in the second sentence). Anyway, the inclusion happens during the execution of the script, when (and if) the `include` statement is reached. The included file is compiled first, separate from the script that includes it and its syntax must be valid. Some language constructs (the [namespace aliases](http://php.net/manual/en/language.namespaces.importing.php) f.e.) are processed during the compilation and their effect is limited to the file that contains them, they do not interfere with included files or the includer. – axiac Oct 26 '17 at 18:17
  • @axiac Thank you for taking you the time to answer :) Much better than before now :P – Twinfriends Oct 27 '17 at 07:23
  • 3
    **Warning - please do not use obscenities in your posts.** Note that other users may have flagged your post as **rude or abusive** leading to possible rep loss or suspension. Please [Be Nice](https://stackoverflow.com/help/be-nice): *Avoid vulgar terms and anything sexually suggestive* – Mithical Dec 07 '17 at 10:19

3 Answers3

2

PHP is a server-side language, whereas HTML is client-side. This means the PHP code gets executed on the web server before then being passed to the client's browser.

See this question for details on the differences between client-side and server-side programming.

crazyloonybin
  • 935
  • 3
  • 18
  • 36
0

PHP process your request in the server and return you a html file (in this case). That is, you make a request with all informations that you need and it returns you a processed information.

  • So html is parsed first then php in included file? – webionDev Jul 06 '17 at 11:52
  • @MohammadRahman php will read/execute first what ever is on top of the page then move to bottom – Masivuye Cokile Jul 06 '17 at 12:05
  • How php in include gets ID and password if it's executed first @MasivuyeCokile – webionDev Jul 06 '17 at 12:35
  • @MohammadRahman "How php in include gets ID and password if it's executed first" because it is not getting the id and password from the HTML that it is outputting, instead it gets them from the parameters of the request that triggered the execution of the script – Accountant م Jul 06 '17 at 12:48
  • @MohammadRahman When a file is included, the code it contains inherits the variable scope of the line on which the include occurs. Any variables available at that line in the calling file will be available within the called file, from that point forward. – Masivuye Cokile Jul 06 '17 at 12:49
0

"Because user input is from html"

I believe you confuse between the HTML your PHP script is going to output as a response , and between the HTML <form> that triggered the request.

the HTML in your question is not going to be PARSED by the PHP, instead it is going to be outputted as a response to client request that triggered this PHP script.

That confusion may be happened because your PHP script is outputting the HTML form, and also it process the request comes from that HTML form.

PHP is about the work of building the final HTML to output.


For example:

On the client side whether it is an HTML <form> running by a browser , or C# bot, or JAVA application , or whatever program is going to send the http request to your server(PHP script) , suppose you send this request with these parameters

Request URL: "http://www.example.com/index.php"
Request method: "POST"
Request parameters: "username=myname&password=123"

that request is going to trigger the PHP to parse the index.php script on the server


On the Server side your web server is going to execute the index.php after filling out the request parameters so you can use them in your code.

before triggering index.php

$_POST['username'] = $_REQUEST['username'] = "myname";
$_POST['password'] = $_REQUEST['password'] = "123";

now as you have the request parameters let's call index.php

<?php
    include('login.php'); // Includes Login Script

    if(isset($_SESSION['login_user'])){
        //check if user is authentic then redirect him to the profile page.
        header("location: profile.php");
        //you should exit; your code here
        //see: https://stackoverflow.com/questions/2747791
    }
   /*now any string that is not between the php open and close tags <> is parsed as
    HTML that needs to be outputted*/
?>
<!-- for example this HTML is going to be outputted ,and the browser is going to show it
unless you used the location header -->
<!DOCTYPE html>
<html>
    <!-- your log in form here -->
</html>
Accountant م
  • 6,975
  • 3
  • 41
  • 61