dsf4354353452 2015-07-10 13:45
浏览 13

PHP全局设置URL并在每个页面中调用它

I want all my hardcoded URL's will be saved in only 1 php file. Let's name it. Global.php so whenever I have hardcoded variable URLs. I will save it on my Global.php.

For example.

define("MAIN", "http://mainpage.com");
define("LOGIN", "http://mainpage.com/login");
define("REGISTER", "http://mainpage.com/register"):

So for example. I have a Login Page..

I will try to used the Global defined URL so It can avoid hardcoded URLS..

Login.php

include'../Global.php';
                 <ul>
                    <li><a href="<?php echo REGISTER; ?>">REGISTER</a></li>     
                    <li><a href="<?php echo LOGIN; ?>">LOG IN</a></li>
                </ul>

Like above, but the problem is on my Global.php. The domain name "http://mainpage.com" is repeatedly used in Global.. What I want to achieve is to set the domain name in Global.php and the subdomain and directory file of every pages should be concatenate to the Global define Domain only.. How I can do this?

I have research another solution like this:

define("LOGIN", "/login");
define("REGISTER", "/register"):


include'../Global.php';
                 <ul>
                    <li><a href="<?php echo REGISTER; ?>">REGISTER</a></li>     
                    <li><a href="<?php echo LOGIN; ?>">LOG IN</a></li>
                </ul>

But there are times that whenever I click the Register, It directs to

http://mainpage/REGISTER

which is supposedly directs to

http://mainpage/register

Sometimes it directs to http://mainpage/REGISTER and sometimes it directs to http://mainpage/register

How I can avoid that?

I think what's best is.. for example..

In my Global.php I think I should declare the domain page and just concatenate the Global domain page in every subdomain page of my page..

Just like this..

define("MAIN", "http://mainpage/");

And I want to concatenate the global define domain in every page that I have hardcoded URLS. How I can achieve it?

For Example On my index.php (MAINPAGE) There is a button there that named LOGIN and REGISTER

So in my code.. I want to be it like this..

include'../Global.php';
                 <ul>
                    <li><a href="<?php echo REGISTER . (Whatever subdomain name that register is and concatenate to global define domain); ?>">REGISTER</a></li>        
                    <li><a href="<?php echo LOGIN . (Whatever subdomain name that login is and concatenate to global define domain); ?>">LOG IN</a></li>
                </ul>

I have different page for LOGIN and REGISTER.. But I think, I was mistaken by concatenating the Global Define Domain to subdomain and directory file.. How to get the subdomain and directory file of a page and put it into a variable and concatenate it to the Global define

Can somebody help me?

  • 写回答

2条回答 默认 最新

  • doulai8128 2015-07-10 13:53
    关注

    The problem you're facing occurs because your REGISTER constant uses domainless links that start with / that means it will take current domain and add link to it.

    To solve that you can load Global.php file and then define REGISTER constant using already defined domain name.

    For example:

    include'../Global.php';
    // notice that you need to include global.php before using MAIN constant
    define("LOGIN", MAIN . "/login");
    define("REGISTER", MAIN . "/register");
    
    评论

报告相同问题?

悬赏问题

  • ¥100 嵌入式系统基于PIC16F882和热敏电阻的数字温度计
  • ¥15 cmd cl 0x000007b
  • ¥20 BAPI_PR_CHANGE how to add account assignment information for service line
  • ¥500 火焰左右视图、视差(基于双目相机)
  • ¥100 set_link_state
  • ¥15 虚幻5 UE美术毛发渲染
  • ¥15 CVRP 图论 物流运输优化
  • ¥15 Tableau online 嵌入ppt失败
  • ¥100 支付宝网页转账系统不识别账号
  • ¥15 基于单片机的靶位控制系统