douyu2817 2011-04-28 15:21
浏览 6
已采纳

登录前后如何分离应用程序逻辑?

Here is the idea.... ...I have a index.php.... ... The application is have one page only, just like the facebook wall, if the user is logined, they can see their "wall", but if they are not logined, they see a register page.... ....

Should I put all these login in the index.php? or I need to separate two index.php...one is logined_index.php, and notLogined_index.php??

Moreover, how can I prevent someone who is not logined in the index.php? Thank you.

  • 写回答

3条回答 默认 最新

  • douhongxie5436 2011-04-28 15:39
    关注

    If your application will remain small there is no need for a separate file. You can use something like this:

    <?php
        session_start();
        if (!isset($_SESSION['uid']) | $_SESSION['uid']=='') $_SESSION['uid']=0;
    
        if($_SESSION['uid']!=0) {
            if (/* user logs out */) {
                session_destroy();
                /* show register/login form */
            }
            else {
                /* main page code goes here */
            }
        }
        else {
            if (/* register details entered */) {
                if (/* register details valid */) {
                    /* register the user and display it all went successful */
                }
                else {
                    /* wrong register info - display error */
                }
            }
            else if (/* login details enetered */) {
                    if (/* user exists and login correct */) {
                    $_SESSION['uid'] = /* set the id of the logged user */;
                    /* log user in and redirect to main page */
                }
                            else {
                    /* wrong login info - display error */
                }
            }
            else {
                /* show register/login form */
            }
        }
    ?>
    

    This is just a simple code layout to build on.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(2条)

报告相同问题?

悬赏问题

  • ¥20 python忆阻器数字识别
  • ¥15 无法输出helloworld
  • ¥15 高通uboot 打印ubi init err 22
  • ¥20 PDF元数据中的XMP媒体管理属性
  • ¥15 R语言中lasso回归报错
  • ¥15 网站突然不能访问了,上午还好好的
  • ¥15 有没有dl可以帮弄”我去图书馆”秒选道具和积分
  • ¥15 semrush,SEO,内嵌网站,api
  • ¥15 Stata:为什么reghdfe后的因变量没有被发现识别啊
  • ¥15 振荡电路,ADS仿真