Hello guys badly need help here, I have created a custom template in wordpress. Now I also used a session variable to check status which I placed in the index.php, if session variable is 1 then redirect to a specific php file. But wp_direct() doesn't seem to work when I uploaded and installed the custom theme on my wordpress site. Check my codes:
<?php
session_start();
if ((isset($_SESSION['stat']) && $_SESSION['stat'] == '1')) {
wp_redirect('http://example.com/wp-content/themes/customtheme/newpage.php', 301);
exit;
?>
Code above only displays a blank page. Any help would be much appreciated.
UPDATE [SOLVED]: I managed to fix it, instead of using wp_redirect() I used a javascript method for redirection. Working code:
<?php
session_start();
if ((isset($_SESSION['stat']) && $_SESSION['stat'] == '1')) {
echo '<script type="text/javascript">window.location.href = "http://example.com
/wp-content/themes/euro/newpage.php";</script>';
?>
First you have assign newpage.php template in your page and then after you have to pass page id in url which you are assigned page newpage.php template. Here i passed "10".
<?php
session_start();
if ((isset($_SESSION['stat']) && $_SESSION['stat'] == '1')) {
wp_redirect('http://example.com/?page_id=10', 301);
exit;
?>
Try following code May be this will help,
session_start();
if ((isset($_SESSION['stat']) && $_SESSION['stat'] == '1')) {
ob_clean();
wp_redirect('http://example.com/wp-content/themes/customtheme/newpage.php', 301);
exit;
}
Make this file "newpage.php" as a template with name "newpage" and then create a page in backend then use this template for that page and then use the url of that page in wp_redirect() function.
Just use this functional as per below:
ob_clean();
$url = get_home_url() . '/login';
wp_redirect($url);
exit();
Related
I'm sorry I couldn't come up with a more fitting title but here is what hope to achieve with PHP:
I have a page with url www.foo[.]com/mypage
I only want that page or url to be accessible if it comes with ?user=$email so if someone tries to visit the url without ?user=$email, they get redirected somewhere else.
How do I define that condition?
Try this script !!!
if(!isset($_GET['user'])){
header("location:./");
}
elseif(isset($_GET['user']) && $_GET['user']==''){
header("location:./");
}
Hi #Edwin here is the solution for you question.
<?php
if(!isset($_GET['user']) && $_GET['user']!='' && $_GET['user']!=null) {
header("Location: http://www.foo[.]com");
} else {
header("Location: http://www.foo[.]com/somepage");
}
?>
In your mypage Page at TOP(Very First line) just write following code
<?php
if(!isset($_GET['user']) || (isset($_GET['user']) && $_GET['user'] == "") ){
header("Location: http://www.foo[.]com");
}
?>
Here I not only check if $_GET['user'] is available or not, I also check that there mus be some value pass to. Here you also no need to write else{} to continue.
I have a custom PHP form that submits to an API. I am wanting to add this form to my sidebar of my Wordpress site. When I try the custom-form.php page by itself it works fine, submits to the API and redirects.
When I include custom-form.php into my sidebar.php then it doesn't want to redirect but it still submits to the API.
I know header needs to happens before anything else loads but how do I get it to load first if I am including it into the page?
header('Location: http://example.com');
Is there a better way to add my form to a Wordpress sidebar?
Code looks something like this:
<?php
if (isset($_REQUEST['submitted'])) {
$errors = array();
if (!empty($_REQUEST['name'])) {
$name= $_REQUEST['name'];
}
else {$errors[] = 'You forgot to enter your name';}
}
if (isset($_REQUEST['submitted'])) {
if (empty($errors)) {
include 'myapi.php';
header('Location: http://example.com');
}
}
//Rest of code....
//Output validation errors
//HTML Form
Your index.php file would look like this.
ob_start();
/*content of your index.php here*/
ob_flush();
This will cause your Output buffering should start before anything sent/echoed to the browser.
Or you can try another solution by using default redirect function:
<?php wp_redirect( site_url() ); exit; ?>
I have multiple pages on my site, most of which are member only pages which a user should have access to only once logged in.
When a user lands at my page they automatically land on the index/home page (index.php). If a user tried to navigate to dashboard.php which is for members only, then they should be redirected back to index.php so they can log in.
at the top of all of my member pages like dashboard.php and manage_account.php i am including a header.php file like so:
include 'header.php';
once a user is logged in i create the session '$_session['user']'
And i am using the following header redirect to check if the session exists and if it doesn't then redirect that user.
<?php
session_start();
include 'config.php';
if (empty($_SESSION['user'])) {
header('Location: index.php');
exit;
}
?>
My problem is rather than cut and paste a header redirect code to each and every member page I just want to place it in the header.php page as this is being included in all of my member pages including my home page index.php.
however it creates a continuous redirect and does not load the page, it says the web
Probably because the header is included as well in the index, right? You can check for that on the condition before redirecting:
<?php
session_start();
include 'config.php';
if (empty($_SESSION['user']) && parse_url($_SERVER['REQUEST_URI'], PHP_URL_PATH) != '/index.php') {
header('Location: index.php');
exit;
}
?>
You could set an array in your config.php with which pages need be validate and then compare with current page to define if will validate.
For example:
$member_pages = array('dashboard', 'member-page', 'etc');
$current = $_SERVER['REQUEST_URI'];
if (empty($_SESSION['user']) && array_search($current, $member_pages) !== FALSE) {
header('Location: index.php');
exit;
}
Hope it helps!
Within member pages do:
$memberOnly = true;
include 'header.php';
and in header.php:
if (isset($memberOnly)) {
if (empty($_SESSION['user'])) {
header('Location: index.php');
exit;
}
}
In public pages (non-member available) you simply:
include 'header.php'
without worrying about $memberOnly
If i understand your problem correctly, your header.php file is included in every page. Though this header.php file contains code which is executed:
header.php:
<?php
// This code is executed whenever you include this file
session_start();
include 'config.php';
if (empty($_SESSION['user'])) {
header('Location: index.php');
exit;
}
?>
You get a redirection loop, what means that this code is also executed in the index.php page. Maybe the header.php file is included in the index.php file as well.
If you would extract the code to a function and call it only in the pages which require a logged in user, you would avoid this loop.
header.php:
<?php
// The code in this function is not called automatically when the file is included
function redirectToLoginIfNecessary()
{
if (!isset($_SESSION['user'])) {
header('Location: index.php');
exit;
}
}
?>
index.php:
<?php
session_start();
include 'header.php';
// Public accessible pages do not call the function
...
?>
secret.php:
<?php
session_start();
include 'header.php';
// Protected pages do call the function
redirectToLoginIfNecessary();
...
?>
This works for me.
Using header is best, but has to be used before any other content is sent to the browser. Which, for me, developing in schmurdpress, makes it hard to implement.
if ( is_user_logged_in() ) {
echo 'Cool!';
} else {
$url = "https://yourdomain.com/log-in/";
echo '<META HTTP-EQUIV="refresh" content="0;URL=' . $url . '">';
}
You redirect index.php to index.php - if the access file is index.php your redirection shouldn't be fired.
<?php
session_start();
include 'config.php';
$basename = substr(strtolower(basename($_SERVER['PHP_SELF'])),0,strlen(basename($_SERVER['PHP_SELF']))-4);
if ((empty($_SESSION['user'])) && ($basename!="index")) {
header('Location: index.php');
exit;
}
?>
Add this check just before the redirect
if ($_SERVER["PHP_SELF" ] != "index.php")
Hi I have following files:
login.php, index.php, header.php, footer.php, restrictaccess.php
restrictaccess.php has following code:
<?php
session_start();
if (!(isset($_SESSION['user']) && $_SESSION['user'] != ''))
if (!(isset($_SESSION['access']) && $_SESSION['access'] != ''))
{
header ("Location: login.php");
exit();
}
if($_SESSION['access']=="2" ) {
echo 'You are logged on as Manager';
}
if($_SESSION['access']=="1" ) {
echo 'You are logged on as Restricted user';
}
?>
restrictaccess.php, header.php and footer.php is included on every page accept login.php.
Both header and footer also includes restrictaccess.php; will this cause any problem ? Is there anyway so that such includes will only be loaded once ?
Currently when someone logon the message "You are loged on as Manager/Restricted user" is displayed thrice.
Your help will be much appreciated, thank you.
Try using require_once() to include your file. This method checks wether the file was already included.
PHP manual
UGLY method
Use require_once or include_once for restrictaccess.php
Better method
Change restrictaccess.php to only:
<?php
if (!(isset($_SESSION['user']) && $_SESSION['user'] != ''))
if (!(isset($_SESSION['access']) && $_SESSION['access'] != ''))
{
header ("Location: login.php");
exit();
}
?>
Then use session_start() at the top of every client page (the pages that user can browse to like index.php).
Use the code about "You are logged-in as ..." only in the header.php (or wherever it make sense to display.
UPDATE
You can include the above restrictaccess.php anywhere in you files BUT you have to make sure that there is no data sent to the client prior to the header ("Location: login.php"); bit (i.e. if you have any echo, print_r or var_dump... before the header ("Location: login.php"); then PHP will generate an error)
I am having an infinite loop issue with this code. It's checking to see if there is a session variable, if there isn't, I want them to be redirected to the index page.
session_start();
if (! isset($_SESSION['foo'])) {
header('Location: /index.php');
}
exit;
I've tried putting the exit; and exit(); and endif; outside the if loop and inside, all are still giving me the issue of infinite loops. This code is located in the header page, which is then called on every page, so you can only access the index page if you are not logged in. That's the gist of why I want this code to exist in the first place.
Thank you for your help in advance.
Add something to your if statement that would exclude the index.php page. Something like
if(! isset($_SESSION['foo']) && strpos($_SERVER['REQUEST_URI'], 'index.php') === false) {
header('Location: /index.php');
}
If you make that check on the index page, it will redirect loop whenever a session is not set. You need to make sure this check is not made on index.php
I think you're just missing session_start();
page.php :
<?php
session_start(); // this is important
if (! isset($_SESSION['foo'])) {
header('Location: /index.php');
exit;
}
?>
index.php :
<?php
session_start(); // this is important
//stuff
$_SESSION['foo'] = 'stuff';
//other stuff
?>