Page redirection issue in wordpress - php

I want to redirect to another page or website in WordPress. I am using below code In which $page variable contains the URL of the website and want to redirect it with a tag href. Problem is the link is appended to localhost current page URL.
the code is here
$page= get_post_custom_values('projPage');
if($page[0] != ""){
var_dump($page);?>
<p>
Project Page
</p> <?php
} else { ?>
<p><em>Project Page Unavailable</em></p><?php
}

Your URL should have a protocol like HTTP or HTTPS else it is considered as a relative path.
$page[0]= 'http://www.google.com';
if($page[0] != ""){
var_dump($page);?>
<p>
Project Page
</p> <?php
} else { ?>
<p><em>Project Page Unavailable</em></p><?php
}
this one is works fine for me

Related

Check is page is visited (through anchor tag) by only allowed websites

Lets say I have a website abc.com/redirect.php which has an anchor tag which links blank to xyz.com/landing.php.
I want to check if the xyz.com/landing.php is opened only through abc.com/redirect.php .
Get parameters are not allowed as someone may copy it on their website. HTTP referer header is not reliable and is not working in this case.
You can use cookie or session.
Example via cookie
<?php
// redirect.php
if(isset($_COOKIE['visited'])) {
header('Location: xyz.com/landing.php');
}
setcookie('visited', '1');
?>
Example via sesion:
<?php
// redirect.php
session_start();
if(isset($_SESSION['visited'])) {
header('Location: xyz.com/landing.php');
}
$_SESSION['visited'] = 1;
?>
For checking referrer on xyz.com/landing.php you can use global $_SERVER variable. For example:
<?php
// landing.php
if($_SERVER['HTTP_REFERER'] == "http://example.com/redirect.php") {
// do something
}
?>

Redirect user to previous page after log in using PHP [duplicate]

After successful login, the user should be redirected to the page he came from, let's say he's been browsing a post and wants to log in so he can leave a comment, so he should be redirected to the post he was browsing. So here is what I have:
login.php shows the login form:
<form method="post" action="login-check.php">
... //input for username and password
</form>
The login-check.php checks if the username and pass are entered, does the user exist, or if he's already logged in, and a p parameter is sent to login.php:
<?php
session_start();
if((empty($username) OR empty($password) AND !isset($_SESSION['id_login']))) {
header("Location:login.php?p=1");
exit();
}
elseif (!user_exists($username,$password) AND !isset($_SESSION['id_login'])) {
header("Location:login.php?p=2");
exit();
}
elseif(isset($_SESSION['id_login'])) {
header("Location:login.php?p=3");
exit();
}
?>
then parameter p is sent back to login.php and displays the according message:
<?php
if(isset($_GET['p'])) {
$p = $_GET["p"];
if($p=="1")
echo "<p class=\"red\">You didn't fill the form.</p><br></br>";
if($p=="2")
echo "<p class=\"red\">User exists.</p><br></br>";
if($p=="3")
header("Location: index.php");
}
?>
BUT, instead of going to index.php after successful login, it should go to the page the user has previously been. I've tried in different ways, but it either doesn't work at all or returns to login.php.
A common way to do this is to pass the user's current page to the Login form via a $_GET variable.
For example: if you are reading an Article, and you want to leave a comment. The URL for comments is comment.php?articleid=17. While comment.php is loading, it notices that you are not logged in. It wants to send you to login.php, like you showed earlier. However, we're going to change your script so that is also tells the login page to remember where you are:
header("Location:login.php?location=" . urlencode($_SERVER['REQUEST_URI']));
// Note: $_SERVER['REQUEST_URI'] is your current page
This should send the user to: login.php?location=comment.php%3Farticleid%3D17. login.php should now check to see if $_GET['location'] is populated. If it is populated, then send the user to this location (in this case, comment.php?articleid=17). For example:
// login.php
echo '<input type="hidden" name="location" value="';
if(isset($_GET['location'])) {
echo htmlspecialchars($_GET['location']);
}
echo '" />';
// Will show something like this:
// <input type="hidden" name="location" value="comment.php?articleid=17" />
// login-check.php
session_start();
// our url is now stored as $_POST['location'] (posted from login.php). If it's blank, let's ignore it. Otherwise, let's do something with it.
$redirect = NULL;
if($_POST['location'] != '') {
$redirect = $_POST['location'];
}
if((empty($username) OR empty($password) AND !isset($_SESSION['id_login']))) {
$url = 'login.php?p=1';
// if we have a redirect URL, pass it back to login.php so we don't forget it
if(isset($redirect)) {
$url .= '&location=' . urlencode($redirect);
}
header("Location: " . $url);
exit();
}
elseif (!user_exists($username,$password) AND !isset($_SESSION['id_login'])) {
$url = 'login.php?p=2';
if(isset($redirect)) {
$url .= '&location=' . urlencode($redirect);
}
header("Location:" . $url);
exit();
}
elseif(isset($_SESSION['id_login'])) {
// if login is successful and there is a redirect address, send the user directly there
if($redirect) {
header("Location:". $redirect);
} else {
header("Location:login.php?p=3");
}
exit();
}
Gotchas
You should run some validation against $_GET['location'] before sending the user there. For example, if I tell people who use your site to click on this link: login.php?location=http%3A%2F%2Fmalice.com%2Fevilpage.php... then they will be sent to a foreign URL that will try to do something bad.
Always make sure to use urlencode when passing URLs as $_GET parameters. This encodes special URL characters (such as ?, &, and %) so that they don't break your url (e.g.: login.php?location=comment.php?id=17 <- this has two ?'s and will not work correctly)
When user gets to the login page use this to see where is come from
$_SERVER['HTTP_REFERER']
Then set this value into the session, and when he is authenticated use url from the session to redirect him back. But you should do some checking before, if the url is your site. Maybe he come from another site directly to login :)
You can save a page using php, like this:
$_SESSION['current_page'] = $_SERVER['REQUEST_URI']
And return to the page with:
header("Location: ". $_SESSION['current_page'])
You should probably place the url to redirect to in a POST variable.
Since the login page is a separate page, I am assuming that you want to redirect to the page that the user reached the login page from.
$_SERVER['REQUEST_URI'] will simply hold the current page. What you want to do is use $_SERVER['HTTP_REFERER']
So save the HTTP_REFERER in a hidden element on your form <input type="hidden" name="referer" value="<?= $_SERVER['HTTP_REFERER'] ?>" /> but keep in mind that in the PHP that processes the form you will need some logic that redirects back to the login page if login fails but also to check that the referer is actually your website, if it isn't, then redirect back to the homepage.
Another way, using SESSION
Assign current URL to session (use it on every page)
$_SESSION['rdrurl'] = $_SERVER['REQUEST_URI'];
and in your login page, use
if(isset($_SESSION['rdrurl']))
header('location: '.$_SESSION['rdrurl']);
else
header('location: http://example.com');
use something like
$_SERVER['HTTP_REFERER'];
And if it's a successful login, display a link saying "Click here to go back" and a link to the referrer, and when the page loads, use some javascript to automatically load that page (don't use back() or whatever that function is as it won't re-load the page and it'll appear like the user never logged in.
You can use session to to store the current page on which you want to return after login and that will work for other pages if you maintain session properly. It is very useful technique as you can develop your breadcrumb using it.
you can use this:
$refererpage = $_SERVER['HTTP_REFERER']; //get referer stored in a variable
if (strpos($refererpage, $_SERVER['SERVER_NAME']) == TRUE) { //if the start position of the referer and the server name is equal
$refvar= $refererpage; //create a mew variable to be used to locate header
} else { //if referer's address is not the same as server name
$refvar= "index.php"; //set the variable to another direction for this request
}
and add the header where ever u want as:
header('location: '. $refvr); //set the header location to the referer varaible
You should try something like $_SERVER['HTTP_REFERER'].
You should first get user refer page in a variable using $_SERVER['HTTP_REFERER']; in your login page.
LIKE:
<?php
session_start();
$refPage = $_SERVER['HTTP_REFERER'];
?>
And now when the user clicks to Login then change header location to user refer page
LIKE:
<?php
if(isset($_POST[login])){
session_start();
header('location:' . $refPage);
}
?>
And in this time you should first check that user refers page empty or not because your user can visit direct your login page then your $refPage variable will be empty so after Click to Login page stays here
LIKE:
<?php
if(isset($_POST[login])){
session_start();
$refPage = $_SERVER['HTTP_REFERER']; // get reffer page url
if(empty($refPage)){
header('location: yourredirectpage'); // if ref page is empty then set default redirect page.
}else{
header('location:' . $refPage); // or if ref page in not empty then redirect page to reffer page
}
}
?>
Or you can use input type hidden where you can set value $_SERVER['HTTP_REFERER'];
LIKE:
<input type="hidden" name="refPage" value="<?php echo $_SERVER['HTTP_REFERER']; ?>">
And when a user clicks to Login then you can get the refPage value and redirect the previous page. And you should also check empty refer page. Because your user can visit direct your login page.
Thank you.
I have created a function to store URL of previous page
//functions.php
function set_previous_page_url(){
$current_url = (isset($_SERVER['HTTPS']) && $_SERVER['HTTPS'] === 'on' ? "https" : "http") . "://{$_SERVER['HTTP_HOST']}{$_SERVER['REQUEST_URI']}";
$previous_url = $_SERVER['HTTP_REFERER'];
if (!($current_url === $previous_url)){
$_SESSION['redirect_url'] = $previous_url;
}
if(isset($_SESSION['redirect_url'])){
$url = $_SESSION['redirect_url'];
return $url;
} else {
$url = "index.php";
return $url;
}
}
And call this function in login.php
// login.php
<?php
// set previous page url to redirect after login
$url = set_previous_page_url();
if(ifItIsMethod('post')){
if(isset($_POST['username']) && isset($_POST['password'])){
if (login_user($_POST['username'], $_POST['password'])) {
redirect($url);
//unset session defined in set_previous_page_url() function
if(isset($_SESSION['redirect_url'])){
unset($_SESSION['redirect_url']);
}
}
}
}
?>
Construct the form action such that it 'remembers', or persists, the previous page by writing out a returnurl=value query string key/value pair to the URL - this can be passed from any page that redirects to login.
I think you might need the $_SERVER['REQUEST_URI'];
if(isset($_SESSION['id_login'])) {
header("Location:" . $_SERVER['REQUEST_URI']);
}
That should take the url they're at and redirect them them after a successful login.
how about this :: javascript+php
echo "<script language=javascript> javascript:history.back();</script>";
it will work same as the previous button in your browser
Use hidden input in your login page.
Like:
<input name="location" value="<?php if(!empty($_SERVER['HTTP_REFERER'])) echo $_SERVER['HTTP_REFERER']; else echo 'products.php'; ?>" type="text" style="display: none;" />
You can try
echo "<SCRIPT>alert(\"Login Successful Redirecting To Previous Page \");history.go(-2)</SCRIPT>";
Or
echo "<SCRIPT>alert(\"Login Successful Redirecting To Previous Page \");history.go(-1)</SCRIPT>";
#philipobenito's answer worked best for me.
I first created a hidden input that contain the user's HTTP referer
<input type="hidden" name="referer" value="<?= $_SERVER['HTTP_REFERER'] ?>" />
and after a successful login i redirected the users to whatever value was stored in that hidden input
$_POST = filter_input_array(INPUT_POST, FILTER_SANITIZE_STRING);
if(!empty($_POST['referer'])){
header('Location: '.$_POST['referer']);
}
else{
header('Location: members.php'); //members.php is a page used to send a user to their profile page.
}
exit;

PHP - Detect homepage in template of script?

How to detect home page in php from main index.php of script. So I have that script. This script is main page of script (not template's index.php
<?php
$view->title = $LANG["HOMEPAGE_TITLE"];
$view->description = $LANG["HOMEPAGE_DESCRIPTION"];
$view->keywords = $LANG["HOMEPAGE_TAGS"];
if ($view->nav == 'homepage') {
// ONLY FOR HOME PAGE
$view->header = $view->render(DIR_TEMPLATE.WEBSITE_TEMPLATE.'/header.php');
$view->footer = $view->render(DIR_TEMPLATE.WEBSITE_TEMPLATE.'/footer.php');
} else {
// FOR OTHER ALL PAGE
$view->header = $view->render(DIR_TEMPLATE.WEBSITE_TEMPLATE.'/header-common.php');
$view->footer = $view->render(DIR_TEMPLATE.WEBSITE_TEMPLATE.'/footer-common.php');
}
echo $view->render(DIR_TEMPLATE.WEBSITE_TEMPLATE.'/index.php'); //This file home page file
I have enter this code but it doesn't detect home page
And I have put this code to index.php (home page). How can I detect is homepage? I have put nav id but it also could not detect.
<?php echo $header;?>
//HOME PAGE CONTENT
<?php echo $footer;?>
I have come across the same scenario while doing a project.
This worked for me.
<?php
$current_file= explode('/', $_SERVER['SCRIPT_NAME']);
$current_file= end($current_file);
if($current_file=='index.php'){
// YOUR LOGIC FOR HOME PAGE
} else {
// YOUR LOGIC FOR ALL OTHER PAGE
}
?>
What this script basically does is it will split the url of the form
http://www.somewebsite.com/index.php
which is provided by
$_SERVER['SCRIPT_NAME']
The splitting is done by the character ' / '.
So the result will be an array of strings.
http , ' ' , www.somewebsite.com , index.php
We are then checking if the last component of the array is "index.php".
This way we are detecting whether the page is home page or not.

Wordpress PHP if iframe external domain then

I try to make something like title say's.
Some friendly websites use a widget with iframe of my front page and i get traffic.
The problem is that all the traffic goes to home page. I want to share the visitors of the iframe to random posts without to edit the external iframe widget of the friend's site's.
i have search and finded a code but has a bug and with this code i cant access my homepage, the home page is redirecting all visitors to random posts.
How can i make the home page redirecting if it is only in iframe?
<?php
$continue = 0;
if(isset($_SERVER['HTTP_REFERER'])) {
//correct domain:
$ar=parse_url($_SERVER['HTTP_REFERER']);
if( strpos($ar['host'], 'mydomain.com') === false ){
} else {
$continue = 1;
}
}
if($continue == 0){ ?>
<?php if (is_front_page()) { ?>
<?php query_posts( 'posts_per_page=1&orderby=rand' ); ?>
<?php while ( have_posts() ) : the_post(); ?>
<?php wp_redirect ( get_permalink () ); exit; ?>
<?php endwhile; ?>
<?php } ?>
<?php } ?>
Thanks
Your code is confusing. Why do you need to store "continue" as a variable? Why don't you just add something like this to the top of your page?
<?php
if(isset($_SERVER['HTTP_REFERER'])) {
$ar=parse_url($_SERVER['HTTP_REFERER']);
if( strpos($ar['host'], 'mydomain.com') === false ){
header('Location: http://www.yourwebsite.com/posts_per_page=1&orderby=rand');
exit();
}
}
?>
(Just reusing your code - basically, check if the site is loaded in an iframe and the homepage was requested - if so, redirect to the random page)

Display html content only in home page

I am developing a site, I was wondering if I could use PHP in a generic way in order to show a div on the home page, but no on any other page. The code I have so far,
<?php
if host == 'http://domain.com/'; echo {
This should only be shown on the homepage http://domain.com but not on domain.com/directory or sub.domain.com/ !
} else {
};
?>
<?php
$match_domains = array('google.com','www.google.com');
if(in_array($_SERVER['SERVER_NAME'], $match_domains) {
echo 'show on my domain!';
}
?>
Using $_SERVER['SERVER_NAME'] we compare it to our desired domain.
We use in_array to search $match_domains for the current domain. If it's in the array, we show our text...Anything else, we ignore.
<?php
$domain = str_replace('www.','',$_SERVER['SERVER_NAME']); // Strip out www.
$match_domains = array('google.com');
if(in_array($domain, $match_domains) {
echo 'show on my domain!';
}
?>
Since you want the home page why don't you check the file name
if ($_SERVER['SCRIPT_NAME'] == "/index.php") {
echo "<div>Content</div>";
}

Categories