duanfuxing2212 2019-02-08 02:24
浏览 49

PHP - 实现添加域名后,某些页面和可单击按钮无法正常工作

when I implement the domain name to my website. some of the other pages and some clickable buttons are not working. Why is this happening? and any solution for this kind of problem? I just implement my domain name which the replace localhost to www.name.com then this problem occurred.

  • 写回答

1条回答 默认 最新

  • duanbu9345 2019-02-08 06:44
    关注

    Since you have no example of your actual script, one must fly a little blind when answering however, I ran into this same issue when I was first starting out creating sites. I came to a couple conclusions:

    1) Create a config file located in the domain root that goes on every top level page that includes reusable items such as domain root path, important folders, session start, maybe database connection credentials, stuff like that. It helps keep your scripts consistent and you always know that you have access to the same things:

    /config.php

    <?php
    define('DS', DIRECTORY_SEPARATOR);
    define('ROOT_DIR', __DIR__);
    define('FUNCTIONS', ROOT_DIR.DS.'functions');
    define('VENDORS', ROOT_DIR.DS.'vendors');
    define('BASE_URL', 'https://www.example.com');
    # Start session
    session_start();
    # Create class loader
    spl_autoload_register(function($class){
        # Turn a class name/namespace into a path
        $path = VENDORS.DS.trim(str_replace('\\', DS, $class), DS).DS.'.php';
        # Include file
        if(is_file($path))
            include_once($path);
    });
    # If you use predominantly functions, create a loader for that (see below)
    include_once(FUNCTIONS.DS.'autoload.php');
    # Add the the base_url function (as noted by others), since it's going to be used a lot
    autoload('base_url');
    

    2) Create a couple handy functions (or classes) including a base url (as mentioned by others):

    /functions/base_url.php

    <?php
    function base_url($path = false)
    {
        return BASE_URL.$path;
    }
    

    If you are using a function-based system, create a function loader:

    /functions/autoload.php

    <?php
    function autoload($func)
    {
        if(!is_array($func))
            $func = [$func];
    
        foreach($func as $function) {
            if(!function_exists($function)) {
                if(is_file($file = FUNCTIONS.DS.$function.'.php'))
                    include_once($file);
            }
        }
    }
    

    3) When creating links and domain paths, use the base_url():

    <a href="<?php echo base_url('/contact/') ?>">Contact</a>
    

    Page html might look something like this:

    <?php
    include_once(__DIR__.DIRECTORY_SEPARATOR.'config.php');
    ?><!DOCTYPE html>
    <html>
    <head>
    <title>Home page</title>
    <link rel="stylesheet" href="<?php echo base_url('/media/css/styles.css') ?>" />
    <script src="<?php echo base_url('/media/js/scripts.js') ?>"></script>
    </head>
    <body>
        <nav id="main-menu">
            <a href="<?php echo base_url('/') ?>">Home</a>
            <a href="<?php echo base_url('/about/') ?>">About</a>
            <a href="<?php echo base_url('/contact/') ?>">Contact</a>
        </nav>
        <img src="<?php echo base_url('/media/images/banner.jpg') ?>" id="banner-image" />
    </body>
    </html>
    

    Anyway hopefully this is useful.

    评论

报告相同问题?

悬赏问题

  • ¥15 AT89C51控制8位八段数码管显示时钟。
  • ¥15 真我手机蓝牙传输进度消息被关闭了,怎么打开?(关键词-消息通知)
  • ¥15 下图接收小电路,谁知道原理
  • ¥15 装 pytorch 的时候出了好多问题,遇到这种情况怎么处理?
  • ¥20 IOS游览器某宝手机网页版自动立即购买JavaScript脚本
  • ¥15 手机接入宽带网线,如何释放宽带全部速度
  • ¥30 关于#r语言#的问题:如何对R语言中mfgarch包中构建的garch-midas模型进行样本内长期波动率预测和样本外长期波动率预测
  • ¥15 ETLCloud 处理json多层级问题
  • ¥15 matlab中使用gurobi时报错
  • ¥15 这个主板怎么能扩出一两个sata口