Page 1 of 1

Automatic login for a specific ip address

PostPosted: November 16th, 2011, 4:40 am
by tasitasi
HI all,

I have a question about the s2member plugin. I have a large organisation looking to access my blog. All traffic from that organisation has the same static ip address, but its multiple computers/users within that organisation.

Can s2member automatically login a user from a specific ip address. For example if someone in that organisation is trying to access an s2member protected page, can s2member check the ip address, and if the ip address is "allowed" access to that page, log them in automatically?

What I am trying to avoid is many users from the same organisation having to continually log in. Ideally users from that organisation/ip address would see the site as if it was free.

Any info would be fantastic.

Thanks

Chris

Re: Automatic login for a specific ip address

PostPosted: November 16th, 2011, 8:21 am
by Eduan
OK, I am not aware of this function yet, though maybe they will implement it someday soon.
Mean while you can check under WP Admin -> s2Member -> Restriction Options -> Unique IP Access Restriction.

Hope this helps. :)

Re: Automatic login for a specific ip address

PostPosted: November 16th, 2011, 1:29 pm
by Bruce C
Yes, this is possible. However it would take a bit of code to do this.

What you can do is create a hook in 'wp-head'/the <head> part of WordPress when a user is viewing a page. Then you can check if the page is your membership options page, using the is_page() conditional. You'll need the page Id. I recommend getting WP Show ID's to do this.

Here's a code snippet that'll do what you need. Keep in mind that you need to update the value of the variables I set:

Code: Select all
<?php
function auto_login_for_specific_ip 
()
    {
        //Change this to the correct page id for your Membership Options Page
        $page_id = 10;
        //Customize this to the static IP you mentioned earlier
        $user_ip = '127.0.0.1';
        //Customize this to the correct user login for the users
        $user_login = 'demo';
        //Customize this to the correct password.
        $user_pass = 'demo';
        //If the user is logged in, or if the IP doesn't match, exit the script
        if (is_user_logged_in () || $_SERVER['REMOTE_ADDR'] != $user_ip)
            exit ();
        //To prevent errors
        wp_reset_query();
        //Check to see if page is correct.
        if (is_page ($page_id))
            {
                //Should be set if the user is trying to access a protected page.
                $redirect = $_GET['s2member_seeking'];
                //To find the correct page/post id's
                    $redirect = str_replace (array('page-', 'post-'), array('?page_id=', '?p='), $redirect);
                //Send Login Details
                echo '<form id="autologin" action="' . get_option ('siteurl') . '/wp-login.php?redirect_to=/' . $redirect . '" method="post"><input type="hidden" name="log" value="' . $user_login . '"/><input type="hidden" name="pwd" value="' . $user_pass . '"/></form>';
                echo '<script type="text/javascript"> function javaautolog () { var frm = document.getElementById("autologin"); frm.submit(); } window.onload = myfunc;</script>';
            }
    }
add_action ('wp_head', 'auto_login_for_specific_ip');
?>


Put this in your s2hacks.php file ( if you don't have one, make one; under wp-content\mu-plugins\s2hacks.php )

Hope that helps!

Re: Automatic login for a specific ip address

PostPosted: November 16th, 2011, 1:35 pm
by Eduan
OK, thanks Ace! :)

Re: Automatic login for a specific ip address

PostPosted: November 16th, 2011, 5:13 pm
by tasitasi
Thanks guys for your input! I will try this out today and report back once I have had a chance to test it. :)

Re: Automatic login for a specific ip address

PostPosted: November 17th, 2011, 5:36 am
by tasitasi
Hi guys,

So i tried this but it does not work for me. I created the folder mu-plugins, put the code in s2hacks.php and wordpress picked it up as a must use plugin. First I tried it locally on my wampserver copy of the site and it caused all pages to not load. The html source ends with <meta name="generator" content="WordPress 3.2.1" />

I then tried it on my live site and it did the same thing but only for the page I want it to log in to. All other pages load fine.

I also tried changing the page id to an unrestricted page, and that unrestricted page loads fine, but does not log me in. Again with this none of the protected pages load however. They also end <meta name="generator" content="WordPress 3.2.1" />

Maybe this info can shed some light on something I am doing wrong or something up with the code?

Could another option be to create a link to a dummy page that runs some of this auto login code, but just for anyone using the link? I would really appreciate any info to get this going :)

Thanks

Re: Automatic login for a specific ip address

PostPosted: November 28th, 2011, 1:22 am
by tasitasi
Hi all, I am still having trouble with this and desperately need a solution. I am using this on localhost using wampserver. My ip is 127.0.0.1. Is there anything I could be missing? I have tried all of the different member types, it just never logs me in automatically.

Is there anything wrong with this?:

<form id="autologin" action="http://localhost/nzgsd/wp-login.php?redirect_to=/" method="post"><input type="hidden" name="log" value="admin"/><input type="hidden" name="pwd" value="password"/></form><script type="text/javascript"> function javaautolog () { var frm = document.getElementById("autologin"); frm.submit(); } window.onload = myfunc;</script>

Im not a php expert by any means. But it appears it either does nothing, or takes me to a blank page with the header cut off when this script come in.

Perhaps there is another option?

Re: Automatic login for a specific ip address

PostPosted: November 28th, 2011, 2:26 am
by Bruce C
Try this:

Code: Select all
    <?php
    function auto_login_for_specific_ip ()
        {
            //Change this to the correct page id for your Membership Options Page
            $page_id = 10;
            //Customize this to the static IP you mentioned earlier
            $user_ip = '127.0.0.1';
            //Customize this to the correct user login for the users
            $user_login = 'demo';
            //Customize this to the correct password.
            $user_pass = 'demo';
            //If the user is logged in, or if the IP doesn't match, exit the script
            if (is_user_logged_in () || $_SERVER['REMOTE_ADDR'] != $user_ip)
                exit ();
            //To prevent errors
            wp_reset_query();
            //Check to see if page is correct.
            if (is_page ($page_id))
                {
                    //Should be set if the user is trying to access a protected page.
                    $redirect = $_GET['s2member_seeking'];
                    //To find the correct page/post id's
                        $redirect = str_replace (array('page-', 'post-'), array('?page_id=', '?p='), $redirect);
                    //Send Login Details
                    echo '<form name="autologin" action="' . get_option ('siteurl') . '/wp-login.php?redirect_to=/' . $redirect . '" method="post"><input type="hidden" name="log" value="' . $user_login . '"/><input type="hidden" name="pwd" value="' . $user_pass . '"/></form>';
                    echo '<script type="text/javascript">document.autologin.submit();</script>';
                }
        }
    add_action ('wp_head', 'auto_login_for_specific_ip');
    ?>

Re: Automatic login for a specific ip address

PostPosted: November 28th, 2011, 4:06 am
by tasitasi
hmm thanks, but this has pretty much the same problem for me.

I tried it live on the actual site and for anyone with any other IP it just shows blank screens for each page.
For me (im using my ip address in the script) I can see the unrestricted pages fine, any s2member protected page is blank, and the page of the page ID I put in the page_id var redirects to "http://www.nzgsd.co.nz/wp-login.php?redirect_to=/", but its a blank page where without the script that link shows the login page. It does not log me in either.

The php looks all good to me - not that I would know any different, I am just stumped by this. Any other suggestions I would really appreciate! :)

Re: Automatic login for a specific ip address

PostPosted: November 28th, 2011, 9:11 am
by Eduan
Have you configured under WP Admin -> s2Member -> General Options -> Localhost WAMP/MAMP Developers?

Re: Automatic login for a specific ip address

PostPosted: November 28th, 2011, 5:07 pm
by tasitasi
Yeah I have tried that, no luck with that either. I have narrowed the problem down slightly - when I try to access a restricted page, it does the redirect, but when it is done the page doesnt load. And the source code stops at <meta name="generator" content="WordPress 3.2.1" />

I have tried so many different things and have not once had it log me in automatically. Is driving me crazy, but for now I have a workaround, which unfortunately involves duplicating the entire site somewhere else and redirecting certain ip addresses to that. Yuck!

Re: Automatic login for a specific ip address

PostPosted: December 1st, 2011, 1:20 am
by Cristián Lávaque
Have you added any hacks or edited any of the source files? If so, could you make sure you don't have a space outside the <?php ?> tags? This could be causing the blank page.

Do you have an error log from the server or PHP?

Re: Automatic login for a specific ip address

PostPosted: December 5th, 2011, 6:17 am
by tasitasi
Hi Cristian, I am not using any other hacks and I have not touched any of the source files. Its a very standard install of s2Member. From what I could gather the script provided should work, and probably does on a clean WP and s2Member install. I havnt had time to test this. Perhaps it is a conflict with another plugin, a couple of them mess with the header tag also:

All in One SEO Pack
Contact Form 7
Google Analytics for Wordpress
Relevanssi
Sidebar Login
Widget Logic
Wordpress Importer
WP Show IDs

When I get a chance I will go through and start from scratch adding the plugins etc. Ill report back if I find a culprit/can get it working.

Perhaps one day we will see this option available natively in s2 :) ?