PHP Redirect - IF with multiple conditions - php

I'm having an issue with something quite simple and I have no idea why.
Need a fresh pair of eyes.
In my configuration file I am pulling a users profile information, if their profile is incomplete and they are not in the: home, settings, logout, profile error page then they will be redirected.
Basically I am making it mandatory or they won't be able to navigate to other areas of the system.
$link = $_SERVER["REQUEST_URI"];
if($counter<9 && ($link !="home" OR $link !="logout" OR $link !="profileError" OR $link !="profileSettings")){
header('Location: profileError');
kill();
}
I've tested my counter which seems to be working fine.
Any help would be appreiated!

$_SERVER['REQUEST_URI'] contains the full pathname of the file from the URL, e.g. /home.php or /folder/logout.php. So none of your $link tests will work. Try:
$link = basename($_SERVER['REQUEST_URI'], '.php');
to get just the filename without the .php extension.

Hi try to debug your code first echo $link and $counter both or add else to see whats wrong
$link = $_SERVER["REQUEST_URI"];
//echo $link; echo $counter;
if($counter<9 && ($link !="home" OR $link !="logout" OR $link !="profileError" OR $link !="profileSettings")){
header('Location: profileError');
kill();
}
else{
echo "there is some error"."</br>";
echo $link; echo $counter;
}

Comment your condition and test the header location, if the header location does not work, check your condition if don't have something error.

Managed to get it working.
$link = basename($_SERVER['REQUEST_URI'], '.php');
if($counter <9){
if($link !="home" OR $link !="logout" OR $link !="profileError" OR $link !="profileSettings"){
header('Location: profileError');
kill();
}
}
Not sure why splitting the IF statements helped but at least it works.

You should have used && condition operator instead of || (OR).
Translated : If the page is not called X and Y then ... Otherwise, If the page is not called X or Y then... it would always be called.
$link = basename($_SERVER['REQUEST_URI'], '.php');
if($counter <9){
if($link !="home" && $link !="logout" && $link !="profileError" && $link !="profileSettings"){
header('Location: profileError');
kill();
}
}

Related

I want to redirect website visitor from "expl.com/... "to "expl.com/index.php/..."

This is my domain http://cdtr.cf it lands on index.php
Where I wrote some code which pulls the URL from URL box and trim its suffix to identify visitor's identity.
The Problem is when someone opening http://cdtr.cf/.... it shows error 404 page not found
while just http://cdtr.cd/ is working fine. All I need is When someone visit with http://cdtr.cf/.... they must be redirected to index.php and their request should be processed like http://cdtr.cf/index.php/.....
I want to keep the suffix anyhow for some purpose :-http://cdtr.cf/suffix.
It will be great if all this happen without any visible change in URL box.
Thanks in advance.
$link= (isset($_SERVER['HTTPS']) && $_SERVER['HTTPS']=== 'on'? "https" : "http") . "://" . $_SERVER['HTTP_HOST'] . $_SERVER['REQUEST_URI'];
echo $link; //It is working fine on localhost but not when i put it live
This code will give you the rest of the url as per your requirement
<?php
$link = $_SERVER['REQUEST_URI'];
echo $link = ltrim($link, '/');
//if you want you can trim last / too using $link = rtrim($link, '/');
?>
If you want to get middle stuff like this yoursite.com/stuffs/removedthis
then you have to use below code
<?php
$link = $_SERVER['HTTP_HOST'];
$link .= $_SERVER['REQUEST_URI'];
$link = explode('/', $link);
echo $a = $link[1];
?>
Second example:
$link = "example.com/stuff/removethis/....blah";
$link = explode('/', $link);
echo $a = $link[1];

Instantly redirect to URL that has been scraped from google search results

Recently, I was wondering if the web scraper written in PHP could instantly redirect to the 1st url fetched on google search.
<?php
include('simple_html_dom.php');
$html = file_get_html('https://www.google.com/search?q=raspberry&oq=raspberry&aqs&num=1');
$linkObjs = $html->find('div[class=jfp3ef] a');
foreach ($linkObjs as $linkObj) {
$title = trim($linkObj->plaintext);
$link = trim($linkObj->href);
//if it is not a direct link but url reference found inside it, then extract
if (!preg_match('/^https?/', $link) && preg_match('/q=(.+)&sa=/U', $link, $matches) && preg_match('/^https?/', $matches[1])) {
$link = $matches[1];
} else if (!preg_match('/^https?/', $link)) { // skip if it is not a valid link
continue;
}
echo $link . '</p>';
}
?>
The code fetches 1st top result from the google search of "raspberry" and prints the url of that site. I want it to redirect it to that url and not print it.
Use the php's built-in header() function. You will use it like this:
header("Location: $link");
Please note that if the link in the $link variable doesn't have a http or https prefix it may not redirect correctly so you may want to check if it exists first. If it doesn't just preprend it and do the redirect.
Also, don't use echo or any other statement that outputs something to the screen before calling the header() function because that won't work either.

Redirect in PHP doesnt load my link?

I have 2 php files.
The first one handles the link and stuff. Here is a preview my code:
<?php
session_start();
$link=$_GET['redirect'];
if ($link == '1'){
$_SESSION['link'] = 'https://www.google.com/';
header('Location: selection_handler.php');
exit;
}
if ($link == '2'){
$_SESSION['link'] = 'https://www.facebook.com/';
header('Location: selection_handler.php');
exit;
}
if ($link == '3'){
$_SESSION['link'] = 'https://www.twitter.com/';
header('Location: selection_handler.php');
exit;
}
?>
This is my selection_handler.php
<?php
session_start();
$link= $_SESSION['link'];
if(isset($_SESSION['user_id']))
{
header("Location: " .$link);
exit;
}
else
{
echo $_SESSION['link'];
}
?>
user_id would be handled later so by default, i would get the echo of the link from the session, right? that works properly but when i try to test setting a value for the user_id, i see my browser trying to load the link. It says 'Resolving host... Waiting for [insert link selected]' but doesnt fully continue to the site but instead im shown a blank page?
UPDATE: I tried changing the header to ('Location: https://www.google.com') instead of getting from the variable but im still getting a blank page

PHP if/else is not functioning properly

I am trying to use an if/else statement in PHP. Currently what I am trying to do is if the $_SESSION['usr']; is equal to the current directory ($dir_auth2) variable that the user is trying to access. Then they can access the directory or index.php I have in it. Else, if the $_SESSION['usr']; is != to the current directory, then redirect them to home page. Currently, when a user types in somebody else's directory, that is not theres they can access it.
<?php
session_name('tzLogin');
session_set_cookie_params(2*7*24*60*60);
session_start();
//This if statement below is the problem
if($_SESSION['usr'] == $dir_auth1) {
//This demo.php is the home page
header("Location: demo.php");
} else {
echo "You are logged in as " . $dir_auth1;
}
$dir = getcwd();
$dir1 = str_replace('/home/pophub/public_html/', '/', $dir);
$dir_auth = getcwd();
$dir_auth1 = str_replace('/home/pophub/public_html/gallry/', '', $dir_auth);
echo $_SESSION['usr'];
echo $dir_auth1;
$dir_user = getcwd();
$dir_user1 = str_replace('/home/pophub/public_html/gallry', '', $dir_user);
?>
Either you haven't posted the whole script or you don't define $dir_auth2 anywhere. Which is bad since you rely on its value in
if($_SESSION['usr'] == $dir_auth2) {
Also, you should use die() after calling header()
header("Location: demo.php");
die();
How to make a redirect in PHP?
I think this is what you're looking for.
You need to define the variable $dir_auth1 before trying to use it in the if/else statement.
Also I think what you want is != instead of ==
<?php
session_name('tzLogin');
session_set_cookie_params(2*7*24*60*60);
session_start();
$dir = getcwd();
$dir1 = str_replace('/home/pophub/public_html/', '/', $dir);
$dir_auth = getcwd();
$dir_auth1 = str_replace('/home/pophub/public_html/gallry/', '', $dir_auth);
$dir_user = getcwd();
$dir_user1 = str_replace('/home/pophub/public_html/gallry', '', $dir_user);
if($_SESSION['usr'] != $dir_auth1) {
header("Location: demo.php");
} else {
echo "You are logged in as " . $dir_auth1;
}
?>
Also you can combine all of your string functions into one like so:
$dir_auth1 = str_replace(array("/home/pophub/public_html/","/home/pophub/public_html/gallry/"),"",getcwd());

Determine Logo link based on URL using PHP

Situation is getting a logo on:
domain.com/special_dir/any_page
or
domain.com/special_dir/any_dir/
to use a link to [domain.com/special_dir/].
Everywhere else on [domain.com/] the logo must a link to [domain.com/]
This is what I have so far.
<?php
$host = $_SERVER['SERVER_NAME'] . $_SERVER['REQUEST_URI'];
if( $host == 'domain.com/special_dir/' ) {
echo '<div"><img src="..."></div>';
} else {
echo '<div"><img src="..."></div>';
}
?>
The logo for [domain.com/special_dir/] only works for [domain.com/special_dir/] URL, no others. I suppose the code it doing what it should, I just don't know how to make it recursive. I did search and read a lot of similar situations but none based on PHP code worked for me.
It is WordPress Multi-site setup and the "special_dir" is a regular sub-directory.
How to correct?
Thanks
Your if ($host == 'domain.com/special_dir/') statement means the special link will be printed for domain.com/special_dir/ only. It excludes everything else, including comain.com/special_dir/any_dir and domain.com/special_dir/any_page.
If think you want ...
if (substr($host,0,22) == 'domain.com/special_dir/') { ... }
This did the trick.
<?php
$url = $_SERVER["REQUEST_URI"];
if (strpos($url, "/special_dir/") === 0) {
echo '<div"><img src="..."></div>';
} else {
echo '<div"><img src="..."></div>';
}
?>

Categories