PHP Redierct with if, if else and else - php

Hello I have a question about php.
I want to create a special redirect script but its not working. I want to check if the 'keyword' is in the list. If in the list than redirect with 'header location' If not redirect to a searchmachine with the value you get from $_get.
<?php
$q=$_get['q']
if ($q = tw) {
header('Location: http://twitter.com');
exit;
} else if ($q = fb) {
header('Location: http://fb.com');
exit;
} else {
header('Location: https://searchit.com/search?q='$q'+ ');
}
?>
I have a list with 10 keyword now like
tw twitter.com
fb facebook.com
gg google.com
etc. all in a text list.

The last part of your code: header('Location: https://searchit.com/search?q='$q'+ '); seem to be the issue with the blank pange, also called "white page of death" :)
Try header('Location: https://searchit.com/search?q=' . $q);
You also forgot a semicolon after: $q=$_get['q']
You could also try this setup:
switch($_GET['q'])
{
case 'tw':
header('Location: http://twitter.com');
exit;
case 'fb':
header('Location: http://fb.com');
exit;
default:
header('Location: https://searchit.com/search?q=' . urlencode($_GET['q']));
}

you should write like
$q=$_GET['q'];
and not $_get it would be $_GET['q'] or you can use $_REQUEST['q']
and use
error_reporting(E_ALL);
ini_set('display_errors', true);
to see if any error on you page.

Related

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

How do I Redirect to Login page using PHP

I am trying to redirect my php login page so that if user is authorised, it goes to a page (r_index.php) and if the user isn't authorised they go back to the login page (login.html).
This is my code:
<?php
if ("password"=="$password") { // Start the condition ?>
Manage classes
<?php } // End the condition ?>
<?php if ("password"=="") { ?>
Login
<?php }
?>.
What am I doing wrong? How should I resolve it?
replace your code with this:
<?php
if ("password"== $password) {
header("location:r_index.php");
}
else if ($password=="") {
header("location:login.html");
}
?>
If you want to redirect you should use:
header('Location: http://www.example.com/r_index.php');
in your code.
<?php
$accessGranted = false;
if($password == 'password') {
$accessGranted = true;
}
if($accessGranted) {
header('Location: r_index.php');
}
else {
header('Location: login.html');
}
exit;
Actually your syntax is wrong, else there is no problem of using HTML inside php. It will work well and good.
Just make sure not to put your variable inside quotes, and change the statement as follows:
if($password=="password")
and
if($Password==" ")

Check url for information with php

I am wanting to check the url of one of my pages if the user has landed correctly
for example if a user visits www.example.com/page.php?data=something
The user will beable to view the page
but if the user visits www.example.com/page.php he gets redirected
This is my current code
$checkurl = 'http://' . $_SERVER['SERVER_NAME'] . $_SERVER['REQUEST_URI'];
if (strpos($check, "?data=")!==false){
}
else {
header("Location: index.php");;
}
I thought this would work and been at this for a while cant seem to see a problem but i am still learning...
You need to use $_GET
For example
if (!isset($_GET["data"])) {
header("Location: index.php");
}
PHP Manual $_GET
Have you tried
if(isset($_GET['data']))
{
}
else
{
header("Location: index.php");
}
This way you just check if there is a "data" on your URL
You can just use this since you are looking for a query string aka a $_GET request:
if ($_GET['data'] != 'something') {
header('Location: http://test.com');
exit();
}
Also, if you just want to check if they included ?data=:
if (!isset($_GET['data'])) {
header('Location: http://test.com');
exit();
}

restrict users with sessions

I am looking to add pages on my site that only people with a certan rank will be able to see while others will be kicked to a different page. What would be a simple way of doing this?
This is what I have right now.
<?php session_start();
$rank=$_SESSION['rank'];
$loggedinusername=$_SESSION['loggedinusername'];
$loggedinuseremail=$_SESSION['loggedinuseremail'];
?>
Thanks
For single allowed rank
if ($rank != 'allowed_rank') {
header('Location: some_other_page.php');
exit;
}
For multiplpe allowed ranks
if (!in_array($rank, array('allowed_rank1', 'allowed_rank2'))) {
header('Location: some_other_page.php');
exit;
}
<?php
session_start();
if ($_SESSION['rank'] > 1) // or whatever your minimum rank is
{
header('Location: highrankpage.php');
}
else
{
header('Location: lowrankpage.php');
}
exit();
?>
You'll want to include this code on every page that you want to protect.

PHP Redirect Loop

On my index page I have a link to my login.php page with this code:
<?php
if(isset($_SESSION['username'])) {
echo "<div id='logout'><a href='logout.php'>Logout (".$_SESSION['username'].")</a></div>";
} else {
echo "<div id='login'><a href='login.php'>Login (Regular)</a></div>";
}
?>
On the login.php page I have
<?php
include('check.php');
$ref = getenv('HTTP_REFERER');
if (isset($ref)) {
header("Location: " . $ref);
exit;
} else {
header("Location: index.php");
exit;
}
?>
check.php is the code for the login form and it checks the users level to make sure they can access the page. I was told that I need to add a check to see if the referral is login.php, otherwise it will go in an infinite loop and I am of course getting "This webpage has a redirect loop". However, I have no clue how to do this and I can't find any information on how to fix it. Anyone know a quick solution?
You should be able to just do
if (isset($_SERVER['HTTP_REFERER']) && end(explode('/',$_SERVER['HTTP_REFERER'])) != 'login.php') {
header("Location: " . $_SERVER['HTTP_REFERER']);
exit;
} else {
header("Location: index.php");
exit;
}
Note that this is a simplified code - you may need to be a bit smarter than that.

Categories