Check url for information with php - 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();
}

Related

How to reload a PHP page twice before displaying it completely to a user?

When a user opens a php page, can I make the page to reload by itself for two times before showing the contents of it to the user?
I tried to use:
header("Location: http://url");
but it goes on loop and never loads the page.
This is veeeery unusual, what I could think of is:
URL: page.php
if (!isset($_GET["time"]) && !isset($_GET["done"]))
{
header("Location: http://url.com/page.php?time=1");
exit;
}
else if ($_GET["time"] == 1)
{
header("Location: http://url.com/page.php?time=2");
exit;
}
else if ($_GET["time"] == 2)
{
header("Location: http://url.com/page.php?done=1");
exit;
}
Or you could use sessions, but good luck with that.

PHP Redierct with if, if else and else

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.

How do i send a user to another page using header if session is empty?

I am making a form over a few pages that will send me an email at the end but i don’t want people going to other pages if they have not inputted their IGN (in game name) so i have tried to put it into a session. My problem is checking the session as i can’t get it to send the user back to the main page if the session is empty here is my code so far.
<?php session_start();
$_SESSION['IGN']=$_POST['IGN'];
if ($_SESSION['IGN']="") {
header('Location: Index.php');
}
?>
Is it that im checking the session wrong? Can you take a look and help me please :-)
Yes, you need to do:
if ( $_SESSION['IGN'] == "" ) { // here you need to use "==" instead of "="
header('Location: Index.php');
}
Read the manual how to compare.
Also you can check in such way:
if (isset($_SESSION['IGN']) && !empty($_SESSION['IGN'])) {
header('Location: Index.php');
}
try this:
<?php session_start();
$_SESSION['IGN']=$_POST['IGN'];
if ($_SESSION['IGN']=="" || is_null($_SESSION['IGN'])) {
header('Location: Index.php');
}
?>

Redirecting URL with elseif $_SERVER['PHP_SELF']

I have a main page that users go to that shows output from a MySQL query depending on the variable passed to it. So;
http://website/mypage.php?page=0
However, I would like to set up redirection so that if someone just goes to
http://website/mypage.php
that it will go to http://website/mypage.php?page=0. I thought of using the following code, which verifies the current page as well as verifies that a user's session is established;
elseif ($_SERVER['PHP_SELF'] == '/mypage.php' && isset($_SESSION['valid_user']))
{
header('Refresh: 0; URL=/mypage.php?page=0');
}
But, this looks to be too general. Is there a way to check for exactly '/mypage.php' or maybe '/mypage.php?page=' ? I thought of using strlen to check for only the 11 characters in /mypage.php, but I'm not sure that this is the most efficient way of doing it.
You can check to see if the variable page has a value and if its empty you can do the redirect
if($_GET['page'] == ''){
header('Location: /mypage.php?page=0');
exit;
}
or
if(!isset($_GET['page'])){
header('Location: /mypage.php?page=0');
exit;
}
In your mypage.php you can check something like this
if(!isset($_GET['page'])) {
header('location: /mypage.php?page=0');
exit;
}
But, I think, instead of redirecting to same page with a get variable, why don't just show the page you want to show by default, when there is no page variable is set.
You should use this way:
header('Location: /mypage.php?page=0');
exit;
Otherwise check the $_SERVER variables for more strict match. http://php.net/manual/en/reserved.variables.server.php
I think probably you need $_SERVER['REQUEST_URI']
Is this what you're looking for?
if(!isset($_GET['page']) || empty($_GET['page'])) {
Header('Location: http://website/mypage.php?page=0')
exit();
}
or
if(!isset($_GET['page']))
$_GET['page'] = 0;
I'm not sure about the second solution, you shouldn't attribute value to $_GET[] variables.
if( !isset($_REQUEST['page']) ) {
header('Location: /mypage.php?page=0');
exit(0);
} else {
if( $_REQUEST['page']=="" ) {
header('Location: /mypage.php?page=0');
exit(0);
}
}

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