dongshao9106 2018-04-17 07:51 采纳率: 100%
浏览 42
已采纳

如何查找登录Wordpress后访问的第一个URL用户

So I've written this reporting function which should show the first URL the user has visited after logging in.

I'm getting the url like so: "http://$_SERVER[HTTP_HOST]$_SERVER[REQUEST_URI]"

I'm using this with the wp_login hook and just saving it to the db.

Perhaps there's a hook that fires right after wp_login?

  • 写回答

1条回答 默认 最新

  • dongmangji0950 2018-04-17 08:41
    关注

    You can use wp_login hook to get user logged in the first URL. Please put the code in your functions.php file.

    function your_function( $user_login, $user ) {
            // your code
            if( ! session_id() ) {
                session_start();
            }
            $_SESSION['counter'] = 1;
            $_SESSION['visited_url'] = "http://".$_SERVER['HTTP_HOST']."/".$_SERVER['REQUEST_URI'];
    
        }
        add_action('wp_login', 'your_function', 10, 2);
        if( ! session_id() ) {
                session_start();
        }
        if(is_user_logged_in()){
        if(isset($_SESSION['counter']) && $_SESSION['counter'] >=2){
            echo $_SESSION['visited_url'];
        }else{
            $_SESSION['counter'] = $_SESSION['counter']+1;
            $_SESSION['visited_url'] = "http://".$_SERVER['HTTP_HOST']."/".$_SERVER['REQUEST_URI'];
        }
        }
    

    How this code works:

    step 1: Once user logged in we set counter and visited_url in the session.

    step 2: check counter variable in session and greater equal to 2.

    step 3: If it meets the condition we print first visited URL.

    It is working code at my side.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 如何用Labview在myRIO上做LCD显示?(语言-开发语言)
  • ¥15 Vue3地图和异步函数使用
  • ¥15 C++ yoloV5改写遇到的问题
  • ¥20 win11修改中文用户名路径
  • ¥15 win2012磁盘空间不足,c盘正常,d盘无法写入
  • ¥15 用土力学知识进行土坡稳定性分析与挡土墙设计
  • ¥70 PlayWright在Java上连接CDP关联本地Chrome启动失败,貌似是Windows端口转发问题
  • ¥15 帮我写一个c++工程
  • ¥30 Eclipse官网打不开,官网首页进不去,显示无法访问此页面,求解决方法
  • ¥15 关于smbclient 库的使用