3 View Detach

Source: Internet
Author: User

Before we explain the application of session and Kookie, we must separate the code appropriately. If the HTML of the page and PHP are written together, then it means that 1 files are responsible for 2 people (the Web designer is responsible for HTML code, the programmer is responsible for PHP code), if the 2 people update the file at the same time, it will cause confusion, in order to facilitate management, the view (front-end HTML) is generally placed separately, and loaded by PHP.
This way we access the PHP file through the browser, the HTML file is loaded by the PHP file.

1 Creating a Folder

Back to our Wlvsoft project, because now is the background writing function, so in the Wlvsoft directory to create the Admin folder, the Resource Resource folder and related files into the admin, and create a protected folder to hold the function module, The directory structure is as shown.

In, there are three folders under the protected folder, where:

    • The controller folder is used to store PHP files that need to be protected, and currently we only have the admin module.
    • The Lib folder is used to store public functions.
    • The template folder is used to store HTML files, and currently only has the admin function module.

Next, put login.html into the Template/admin folder and login.php into the Controlle/admin folder. Then through: http://localhost/wlvsoft/admin/protected/template/admin/login.html access to the login interface, you will see the interface shown.

The CSS, JavaScript, and picture displays that apply to the Login.html interface have no effect, we moved the resource folder before, CSS, JavaScript, and pictures are stored here. So our preliminary judgment is that the resource move caused CSS, JavaScript, and image positioning errors. Modify the CSS, JavaScript, and picture reference code in Login.html, as shown in Listing 1.
Code Listing 1:login.html

......<head>......<!--Reset Stylesheet --<link rel="stylesheet" href="/wlvsoft/admin/resources/css/ Reset.css " type=" Text/css " Media=" screen " /><!--Main Stylesheet --<link rel="stylesheet" href="/wlvsoft/admin/resources/css/ Style.css " type=" Text/css " Media=" screen " />......<link rel="stylesheet" href="/wlvsoft/admin/resources/ Css/invalid.css " type=" Text/css " Media=" screen " /><!--javascripts --<!--jQuery --<script type="text/javascript" src="/wlvsoft/admin/resources/ Scripts/jquery-1.3.2.min.js "></script><!--jQuery Configuration ---<script type="text/javascript" src="/wlvsoft/admin/ Resources/scripts/simpla.jquery.configuration.js "></script><!--facebox jQuery Plugin --<script type="text/javascript" src="/wlvsoft/admin/resources/ Scripts/facebox.js "></script><!--jQuery WYSIWYG Plugin --<script type="text/javascript" src="/wlvsoft/admin/ Resources/scripts/jquery.wysiwyg.js "></script></head><body id="Login"><div id="Login-wrapper" class="PNG_BG">  <div id="Login-top">    <H1>Simpla Admin</H1>    <!--Logo (221px width)--<a href="http://www.865171.cn"><img id="logo" src="/wlvsoft/admin/resources/images/logo.png " alt=" simpla Admin logo " /></a> </div>......

Refresh browsing, landing interface to restore the original effect. However, entering a user name and password to log in will prompt for a 404 error (The page cannot be found), as shown in the interface.

Looking closely, we found that the page that clicked the login request was:
http://localhost/wlvsoft/admin/protected/template/admin/login.php
In other words, when you click Log in, the submitted login.php page is looked up in the current directory, such as a 404 error message if it is not found. We moved the login.php to the Controlle/admin folder earlier, so we need to modify the data in the form of the Login.html page, as shown in Listing 2.
Code Listing 2:login.html

……<form action="/wlvsoft/admin/protected/controller/admin/login.php" method="post">……

Continuing the login will call the code in the login.php to verify the user information and, if entered correctly, you will see a 404 error.
Check the browser path, or file path problem, we will start with index.html into the Admin folder, so verify the jump page needs to modify the path, as shown in Listing 3.
Code Listing 3:login.php

If there is a user, jump to the background first pageif($row){$url="Http://localhost/wlvsoft/admin/index.html";Echo "<script language= ' javascript ' type= ' Text/javascript ' >";Echo "alert (' Log in ... ');";Echo "window.location.href= ' {$url} '";Echo "</script>"; }Else{$url="Http://localhost/wlvsoft/admin/protected/template/admin/login.html";Echo "<script language= ' javascript ' type= ' Text/javascript ' >";Echo "alert (' Username or password is wrong! '); ";Echo "window.location.href= ' {$url} '";Echo "</script>"; }

Note that if the login is successful, but the page is wrong to jump, it is likely that the Firefox cache in the mischief, clean up the cache to re-login on it.

2 Loading views

We have been accessing HTML files in the browser before, and the view separation technique requires that the HTML page be loaded by PHP files. So modify the login.php code, as shown in Listing 4.
Code Listing 4:login.php

<?php    ……    include("D:/software/xampp/htdocs/wlvsoft/admin/protected/template/admin/login.html");?>

When we pass: http://localhost/wlvsoft/admin/protected/controller/admin/login.php
When accessing the login.php file, the login screen will be displayed. Although we do not have direct access to the login.html file, we can use the PHP include embedded view to achieve the Display page effect.

3 Initialization files

I wonder if you have noticed that login.html a string of paths to the resource file? What would you do if I moved the resource folder again? Continue to modify the resource references in login.html, or turn the computer off and sleep as I did.
We want these paths to be like constants, to be used where they are needed, to be modified only once, and all the places where the constants are called can be changed. Here are the definitions you need to know about Web programming:

Constants: Whenever a value is defined (defined only once), it cannot be modified at any time (meaning it cannot be modified during program execution), and in order to differentiate between constants and variables, the name specification of constants is uniformly capitalized and does not add $.
Absolute path: Write the address in full, such as: Include ("d:/software/xampp/htdocs/wlvsoft/admin/protected/template/admin/login.html");
Relative path: Relative to the current file's path, centered on the location of this, such as:/wlvsoft/admin/resources/scripts/facebox.js

In general, the definition field assignment of a constant is managed in a single file, and the init.php file is created in the Protected\lib folder, as shown in Listing 5.
Code Listing 5:init.php

<?php    //定义常量,存放管理员文件夹路径    define("BASE_URL", "http://localhost/wlvsoft/admin/");?>

A global variable is defined in Code Listing 5: Base_url, with a value of: Http://localhost/wlvsoft/admin. How do I apply the init.php file to the login.html page?
Remember how we used the include in login.php to import login.html in front of you. Modify the login.php code, as shown in Listing 6.
Code Listing 6:login.php

<?php    ……    //导入init文件    include("D:/software/xampp/htdocs/wlvsoft/admin/protected/lib/init.php");    include("D:/software/xampp/htdocs/wlvsoft/admin/protected/template/admin/login.html");?>

After importing the initialization file, you can use the constant Base_url defined in the login.html, modify the login.html code, and/wlvsoft/admin/

......<link rel="stylesheet" href="<?php echo base_url? >resources/ Css/reset.css " type=" Text/css " Media=" screen " /><!--Main Stylesheet --<link rel="stylesheet" href="<?php echo base_url? >resources/ Css/style.css " type=" Text/css " Media=" screen " /><link rel="stylesheet" href="<?php echo base_url?> Resources/css/invalid.css " type=" Text/css " Media=" screen " /> <!-javascripts --><!--jQuery --<script type="text/javascript" src="<?php echo base_url?> Resources/scripts/jquery-1.3.2.min.js "></script><!--jQuery Configuration ---<script type="text/javascript" src="<?php echo base_url?> Resources/scripts/simpla.jquery.configuration.js "></script><!--facebox jQuery Plugin --<script type="text/javascript" src="<?php echo base_ URL? >resources/scripts/facebox.js "></script><!--jQuery WYSIWYG Plugin --<script type="text/javascript" src="<?php echo base_url?> Resources/scripts/jquery.wysiwyg.js "></script></head><body id="Login"><div id="Login-wrapper" class="PNG_BG">  <div id="Login-top">    <H1>Simpla Admin</H1>    <!--Logo (221px width)--<a href="http://www.865171.cn"><img id="logo" src="<?php echo base_url? >resources/images/ Logo.png "alt=" simpla Admin logo " /></a> </div>......
4 Magic Constants

The

Previously resolved the problem with reference paths in login.html, but the issue with the include import path has not been resolved. If the path to our Web project deployment changes, the absolute path in the include will be modified as well. To resolve the issue of the include import path, we analyzed the code in Listing 4, and found that when the Web project was redeployed, the files included in the include were susceptible to changes in the following path: d:/software/xampp/htdocs/wlvsoft/, The root directory of the current Web project. We want to have a way to get the current project path dynamically, fortunately PHP provides the magic constants. The
can get the current directory of files by calling system method DirName ( file ), where file represents the magic constant. In order to get the root of the Web project, we create the admin.php under the root directory wlvsoft/, as shown in Listing 8.
Code Listing 8:admin.php

<?php    //定义出一个“系统根路径”的常量    //__FILE__表示文件所在绝对位置,即D:/software/xampp/htdocs/wlvsoft/cms/admin.php    //dirname(__FILE__)表示文件所在目录,即D:/software/xampp/htdocs/wlvsoft    define("ROOT", dirname(__FILE__));  //  __XXX__ : 魔术常量    echo(ROOT);?>

Access this file in the browser, you will see the absolute path to this file, as shown in.

By running it is known that dirname (file) Gets the absolute path of the access file, so that we can use the root constant to replace the easily changing path: d:/software/xampp/htdocs/wlvsoft/.
Modify the admin.php under wlvsoft/to be responsible for initializing the work and jump to the login.php page, as shown in Listing 9.
Code Listing 9:admin.php

<?php  //defines a constant for the "system root Path"  //__file__ indicates the absolute location of the file, i.e. d:/ software/xampp/htdocs/wlvsoft/cms/admin.php  //dirname (__FILE__) indicates the directory in which the file is located, i.e. d:/ Software/xampp/htdocs/wlvsoft  define (, dirname (__file__ )");    //__xxx__: Magic constant     //load initialization  include  (ROOT.         "/admin/protected/lib/init.php" );    //load login.php  include  (ROOT.   "/admin/protected/controller/admin/login.php" ); ?>    

Since the init.php initialization file is included in the admin.php, the import initialization file statement can be removed in login.php, as shown in Listing 10.
Code Listing 10:login.php

<?php    ……    //导入init文件,admin.php中导入,这里注释掉    //include("D:/software/xampp/htdocs/wlvsoft/admin/protected/lib/init.php");    include(ROOT . "/admin/protected/template/admin/login.html");?>

When we include login.html, we use the global variable root, so that the program does not have to make any path changes regardless of the path of the project deployment.

3 View Detach

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.