Protect under-developpement Website / Custom URL per user and store IP - php

I want to protect a page under developpement. The problem is that people are sharing that link. I'd like to manually give a custom link to the users, store the IP they use and then prevent the link being reused on a different IP. I'm using wordpress, and It's the entire site i'd like to protect. Also, is there a way to track who shared the URL?
Exemple :
I'd give a friend this http://exemple.com/abc, that link works as long as the user is on the IP that was first used. If that user shares that link to someone else or another IP tries to access the site using the URL I'd like to log it somehow.

I'll give you some basic advises, since this is relatively complicated and I don't have the time to write all of the code :) .
First you need to add a plain admin page - on this page initially you want to have a <form> with a text field and a submit field. The text field will be where you enter the page URL that you want to share.
You will also have an option stored in the database(we will create that later). It should be an array, to which you will later add the URL's.
Once you submit the URL you will create a nonce for this URL - it must be unique for it(you can use uniqid() for instance). You will then store the URL in an array like this:
if ( $_SERVER['REQUEST_METHOD'] == 'POST' ) {
$urls = get_option( 'my_custom_links', array() );
$id = uniqid();
$urls[ $id ] = array(
'url' => $_POST['my_url'],
'ip' => '',
);
// Update the links.
update_option( 'my_custom_links', $urls );
}
$urls = get_option( 'my_custom_links', array() );
// Display the form here...
// Display the URL's data
foreach ($urls as $id => $data) {
echo '<p>URL: <strong>' . $data['url'] . '</strong><br />Share URL: <strong>' . add_query_arg( 'n', $id, home_url( '/' ) ) . '</strong>' . ( $data['ip'] ? '<br />Accessed from IP: <strong>' . $data['ip'] . '</strong>' : '' ) . '</p>';
}
Then you just need to add a function that will check if a user is allowed to view a specific URL, like so(add the code to your functions.php):
function my_site_protection() {
global $pagenow;
// If we're not on admin or login page and the user is not logged-in
if ( ! is_admin() && 'wp-login.php' != $pagenow ) {
$login_redirect = true;
if ( isset( $_GET['n'] ) && $_GET['n'] ) {
$n = $_GET['n'];
$urls = get_option( 'my_custom_links', array() );
$data = isset( $urls[ $n ] ) ? $urls[ $n ] : false;
if ( $data ) {
if ( ! $data['ip'] || $data['ip'] == $_SERVER['REMOTE_ADDR'] ) {
// If no IP is set, set it and update the option
if ( ! $data['ip'] ) {
$urls[ $id ]['ip'] = $_SERVER['REMOTE_ADDR'];
update_option( 'my_custom_links', $urls );
}
if ( add_query_arg( 'n', $id, $data['url'] ) == curPageURL() ) {
// Don't redirect if we're on the correct page
$login_redirect = false;
} else {
// Redirect the user to the proper URL
wp_redirect( add_query_arg( 'n', $id, $data['url'] ) );
exit;
}
}
}
}
// Redirect user to log-in screen
$login_redirect && auth_redirect();
}
}
add_action('init', 'my_site_protection', 1);
function curPageURL() {
$pageURL = 'http';
if ($_SERVER["HTTPS"] == "on") {
$pageURL .= "s";
}
$pageURL .= "://";
if ($_SERVER["SERVER_PORT"] != "80") {
$pageURL .= $_SERVER["SERVER_NAME"] . ":" . $_SERVER["SERVER_PORT"] . $_SERVER["REQUEST_URI"];
} else {
$pageURL .= $_SERVER["SERVER_NAME"] . $_SERVER["REQUEST_URI"];
}
return $pageURL;
}
You'll have to figure-out the rest on your own, but I gave you most of the stuff, so if you look around a bit, you'll be able to accomplish that.
PP: I haven't tested the code, but in theory it should work - tell me if some part of it doesn't work.

Related

How to redirect user based on page?

I am trying to use below code that whenever user goes to page name called "Through Phone Call" it should direct him to certain page
In functions.php
function my_custom_add_to_cart_redirect( $url ) {
$page_title = $post->post_title;
if ($page_title == "Through Phone Call") {
$url = get_permalink( 2855 );
} else {
$url = get_permalink( 1853 );
}
}
But the code is not functioning what i am missing?
According to the docs you need to call wp_redirect() like below:
function my_custom_add_to_cart_redirect( $url ) {
$page_title = $post->post_title;
if ($page_title == "Through Phone Call") {
$url = get_permalink( 2855 );
} else {
$url = get_permalink( 1853 );
}
wp_redirect( $url );
exit;
}

Cannot find where a redirect is specified

I've recently become the maintainer of a Wordpress site (I'm completely new to wordpress) and I'm having some difficulty determining where a redirect is specified.
I've checked the .htaccess file, and there's nothing specified in there. As far as I can tell, the rewrite rules aren't the cause.
I've tried deleting the page being redirected from and re-creating it, and the redirect still occurs.
My question is: where can you specify a redirect? I've run out of ideas of where to look.
one of my client want to custom url like
https://www.qsleap.com/gmat/resources as you know in wordpress evry request is catch by index.php . request filter catches the request and
call the page .
Read this code it may give you any idea.
function permalinks_customizer_request_before($query ){
$uri=$_SERVER['REQUEST_URI'];
$match= preg_match('/(gmat|gre|sat|lsat|cat)(\/resources\/tags\/)
(.*)\/(articles|videos|concept-notes|qna)/', $uri,$matches);
//$match=
preg_match('/(gmat|gre|sat|lsat|cat)/\resources/\tags/stanford-
gsb/\articles|videos|concept-notes)/?$', $uri,$matches);
if($match){
$url = parse_url( get_bloginfo( 'url' ) );
$url = isset( $url['path']) ? $url['path'] : '';
$request = ltrim( substr( $_SERVER['REQUEST_URI'], strlen( $url ) ), '/' );
$request = ( ( $pos = strpos( $request, '?' ) ) ? substr( $request, 0, $pos ) : $request );
if ( ! $request )
return $query;
$original_url="?page_name=tags&exam=".$matches[1]."&post_tag=".$matches[3]."&post_type=".$matches[4];
if ( $original_url !== null ) {
$original_url = str_replace('//', '/', $original_url);
if ( ( $pos = strpos( $_SERVER['REQUEST_URI'], '?' ) ) !== false ) {
$queryVars = substr( $_SERVER['REQUEST_URI'], $pos + 1 );
$original_url .= ( strpos( $original_url, '?' ) === false ? '?' : '&') . $queryVars;
}
$oldRequestUri = $_SERVER['REQUEST_URI'];
$oldQueryString = $_SERVER['QUERY_STRING'];
$_SERVER['REQUEST_URI'] = '/' . ltrim( $original_url, '/' );
$_SERVER['QUERY_STRING'] = ( ( $pos = strpos( $original_url, '?' ) ) !== false ? substr( $original_url, $pos + 1 ) : '' );
parse_str( $_SERVER['QUERY_STRING'], $queryArray );
$oldValues = array();
global $wp;
$wp->parse_request();
$query = $wp->query_vars;
if ( is_array( $queryArray ) ) {
foreach ( $queryArray as $key => $value ) {
$oldValues[$key] = $_REQUEST[$key];
$_REQUEST[$key] = $_GET[$key] = $value;
$query[$key]=$value;
}
}
$_SERVER['REQUEST_URI'] ='';
$_SERVER['QUERY_STRING']='';
}
}
return $query;
}
add_filter( 'request','permalinks_customizer_request_before',0);
function wp_url_rewrite_templates() {
if (get_query_var( 'page_name' ) && get_query_var( 'page_name'
)=='tags') {
add_filter( 'template_include', function() {
$template= dirname( __FILE__ ) . '/page-tags.php';
return $template;
});
}
}
add_action( 'template_redirect', 'wp_url_rewrite_templates' ,4 );
I think the easiest way for you to remove the redirects will be with this plugin.
https://redirection.me/
After you install it and activate it. From the Wordpress Admin
Tools > Redirection
You'll see a list of redirects, and add/remove any.

dont understand where this action takes in form action wordpress

i am new to wordpress and trying to modify theme. but i am not able to track form action.
<form action="<?php echo appthemes_get_registration_url( 'login_post' ); ?>" method="post" class="login-form register-form custom" name="registerform" id="login-form">
where these action takes ? how data is stored ?
this is the function.php where i get following function code .
function appthemes_get_registration_url( $context = 'display' ) {
if ( current_theme_supports( 'app-login' ) && ( $page_id = APP_Registration::get_id() ) ) {
$url = get_permalink( $page_id );
} else {
$url = site_url( 'wp-login.php?action=register' );
}
if ( ! empty( $_REQUEST['redirect_to'] ) ) {
$url = add_query_arg( 'redirect_to', urlencode( $_REQUEST['redirect_to'] ), $url );
}
return esc_url( $url, null, $context );
}
can anyone help me with this issue?
i am stuck here.
According to the function being used
$url = get_permalink( $page_id );
The above code will get the link of the current custom login page which the theme uses.
Or else it will go to the default WordPress register page
$url = site_url( 'wp-login.php?action=register' );
There is a redirect field in the theme after the registration is over, which makes the user to go to the required page. If it is empty, then it can be skipped or else it is returned to the redirect URL specified.
if ( ! empty( $_REQUEST['redirect_to'] ) ) {
$url = add_query_arg( 'redirect_to', urlencode( $_REQUEST['redirect_to'] ), $url );
}
The data of your registration is stored in a table called users which you can access through PHPmyAdmin

Wordpress rewrite with php

Is possible change this url
www.12345.com/?s=&cp_state=city
to this?
www.12345.com/city
Already try with htaccess but in wordpress it doesen't work
Hi have this code. Is possible change it to have a url like this www.12345.com/city?
add_action('template_redirect', 'search_url_rewrite_rule');
function search_url_rewrite_rule() {
global $wp_rewrite;
if ( is_search() && isset( $_GET['s'] ) ) {
$s = str_replace( array( ' ', '%20' ), '+', get_query_var( 's' ) );
wp_redirect( home_url( $wp_rewrite->search_base . '/' . remove_accents ( $s ) ) );
exit();
}
}
add_action('init','change_search_permalinks');
function change_search_permalinks( ) {
global $wp_rewrite;
$wp_rewrite->search_base = 'search';
}
Thank you in advance
In php level it is not good idea
You can use url shortlink in admin area of wordpress.

Image in recent post of Wordpress Q and A theme

i am using Instant-QA theme, need to update the recent Post widget with image of the user who has asked the questions. I am using the below code in functions.php of subdomain of wordpress website. Currently its showing the simple post--- Any guess in below code-- ?
function print_requested_template_part() {
// Respond only to requests from the same address...
if ( $_SERVER['REQUEST_METHOD'] == 'GET' && isset($_GET['including_template_part']) && isset($_GET['load_part']) && $_GET['load_part'] != '' ) {
$part = $_GET['load_part'];
$func = 'render_' . str_replace('-', '_', $part); // if you have declared a function called "render_footer_include", then "?load_part=footer_include"
if ( function_exists($func) ) {
// Allow for passing parameters to the function
if ( isset($_GET['params']) ) {
$params = $_GET['params'];
$params = ( strpos($params, ',') !== false )? explode(',', $params) : array($params);
call_user_func_array($func, $params);
} else {
call_user_func($func);
}
}
exit; // if we don't exit here, a whole page will be printed => bad! it's better to have empty footer than a footer with the whole main site...
}
}
add_action('init', 'print_requested_template_part', 1);
function render_my_recent_posts( $numberposts = 5 ) { ?>
<ul>
<?php
$args = array( 'numberposts' => '5' );
$recent_posts = wp_get_recent_posts( $args );
foreach( $recent_posts as $recent ) {
echo '<li><a href="' . get_permalink($recent["ID"]) . '" title="Look '.esc_attr($recent["post_title"]).'" >' . $recent["post_title"].'</a> </li> ';
}
?>
</ul><?php
}
DO not waste you time to indulge urself in the above code. Usel below plugin and gets the immediate output.
http://wordpress.org/extend/plugins/recent-comments-with-avatars/ Use this plugin
Thankyou

Categories