dscrb00804 2013-07-23 20:46
浏览 61
已采纳

基于用户ID相关常量返回不同的PHP数组值

I am working with an array of tokens for an HTML template. Two of them ('{SYS_MENU}' and '{SUB_MENU}') are used to generate control buttons for the web application. Right now the buttons show up on the login page before the user's credential's are validated, and I need to change the code so that the buttons are hidden until after users login and reach the main menu. When someone types the http: address into their browser and arrives at the login page the system starts a session for them in the MySQL sessions table with USER_ID = 0. After they login the USER_ID changes to whatever number was assigned to them at initial registration (Example: USER_ID = 54), and after they logout at the end of the session back to 0. Tying this constant to the buttons seems like the best solution and I have found it to work in the past under similar circumstances.

Here is the original array:

$template_vars = array(

    '{LANG_DIR}' => $lang_text_dir,
    '{TITLE}' => theme_page_title($section),
    '{CHARSET}' => $charset,
    '{META}' => $meta,
    '{GAL_NAME}' => $CONFIG['gallery_name'],
    '{GAL_DESCRIPTION}' => $CONFIG['gallery_description'],
    '{SYS_MENU}' => theme_main_menu('sys_menu'),
    '{SUB_MENU}' => theme_main_menu('sub_menu'),
    '{ADMIN_MENU}' => theme_admin_mode_menu(),
    '{CUSTOM_HEADER}' => $custom_header,
    '{JAVASCRIPT}' => theme_javascript_head(),
    '{MESSAGE_BLOCK}' => theme_display_message_block(),
);

The first thing I did was to work with the references directly in the HTML template. I saw an example on w3schools that made it look like you could just type a PHP script into HTML and have it resolve. That didn't do anything except echo a bunch of text randomly into the page. I then found another citation that said you had to activate the PHP with an .HTACCESS entry before it would work directly in HTML. But that didn't close the deal either.

I know that changing '{SYS_MENU}' and '{SUB_MENU}' values in the array to => "", produces the results that I want (I.E. make the menu buttons disappear). So my next thought was I'll create an IF statement that returns two versions of the array based on circumstances, something like:

if(USER_ID != 0)

{

return  $template_vars = //FIRST VERSION OF ARRAY WITH FULL VALUES//

}

else 

{

return $template_vars = //SECOND VERSION OF ARRAY WITH ONLY => ""//

}

But all that did was cause the application load to terminate at a white screen with no error feedback.

My most recent attempt came from something I read here on Stack Overflow. I know that you cannot put IF statements into an array. But the article at this link described a workaround:

If statement within an array declaration ...is that possible?

So I rewrote the array as follows:

template_vars = array(

    '{LANG_DIR}' => $lang_text_dir,
    '{TITLE}' => theme_page_title($section),
    '{CHARSET}' => $charset,
    '{META}' => $meta,
    '{GAL_NAME}' => $CONFIG['gallery_name'],
    '{GAL_DESCRIPTION}' => $CONFIG['gallery_description'],
    '{SYS_MENU}' => ('USER_ID != 0' ? theme_main_menu('sys_menu') : ""),
    '{SUB_MENU}' => ('USER_ID != 0' ? theme_main_menu('sub_menu') : ""),
    '{ADMIN_MENU}' => theme_admin_mode_menu(),
    '{CUSTOM_HEADER}' => $custom_header,
    '{JAVASCRIPT}' => theme_javascript_head(),
    '{MESSAGE_BLOCK}' => theme_display_message_block(),
);

But that seems to have no effect at all. The application doesn't crash but the buttons are static whether you are logged in or logged out.

My question is: What am I missing? I can see that this is possible. But I've been trying things for a day and a half and just seem to be dancing around the solution. Your thoughts would be greatly appreciated.

  • 写回答

1条回答 默认 最新

  • dongzi3805 2013-07-23 21:07
    关注

    The problem here is that you are calling return. With a global include file like this there is not context to return to so the application terminates. What you want to do is just assign the variables.

    if(USER_ID != 0)
    {
       $template_vars = //FIRST VERSION OF ARRAY WITH FULL VALUES//
    }
    else 
    {
       $template_vars = //SECOND VERSION OF ARRAY WITH ONLY => ""//
    }   
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 基于FOC驱动器,如何实现卡丁车下坡无阻力的遛坡的效果
  • ¥15 IAR程序莫名变量多重定义
  • ¥15 (标签-UDP|关键词-client)
  • ¥15 关于库卡officelite无法与虚拟机通讯的问题
  • ¥100 已有python代码,要求做成可执行程序,程序设计内容不多
  • ¥15 目标检测项目无法读取视频
  • ¥15 GEO datasets中基因芯片数据仅仅提供了normalized signal如何进行差异分析
  • ¥100 求采集电商背景音乐的方法
  • ¥15 数学建模竞赛求指导帮助
  • ¥15 STM32控制MAX7219问题求解答