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
Related
This is my code for userslist.php. I put it above the head of this page so if this link is clicked, only admin can enter the page as filtered that is why I have redirections.
session_start();
$loggedInfo['username'] = $_SESSION['username'];
if(
isset($loggedInfo['username']) && $loggedInfo['username']==="admin" &&
trim($loggedInfo['username']) != "guest"
)
{
header('Location: userslist.php');
}
else {
header('Location: ../index.php');
}
This is my php script and I got a problem with redirecting. On the header(location ...) when I changed it to echo true or false, the echo returns the value correctly. But when I put a redirect/location, it does say:
This webpage has a redirect loop
Why is that? :(
Put this code in top of the userlist.php.An try what you got
<?php session_start();
$loggedInfo['username'] = $_SESSION['username'];
if(isset($loggedInfo['username']) && $loggedInfo['username']!="admin"){
header('Location: ../index.php');
exit();
}else if(isset($loggedInfo['username']) && $loggedInfo['username']=="admin"){
?>
You page code here goes
<?php } ?>
You're probably including this code in all pages. Thus on userslist.php it will also redirect to userslist.php. This causes permanent redirects, which is a redirect loop.
This conclusion is however difficult to support without seeing all the code you are using.
i have been following an online tutorial on how to create a login and logout page in php and mysql,which seemed to be way easy not until i stage where i would like to modify the scripts,i am trying to put an admin's page where the user will have access to some links..i added a column in my users table called user_level,i want when a user logs in he his user_level is 1 he will access the links in the season.php script but if his user_level is 2 he will be directed to another page...i tried doing this below but its not working
if($user_level == 1){
header("Location: links.php");
}
else($user_level == 2){
header("Location: client.php");
}
this is my session.php code
<?php
if(!isset($_SESSION['name'])&&isset($_COOKIE['testsite'])){
$_SESSION['name'] = $_COOKIE['testsite'];
}
$dir = "profiles/".$_SESSION['name']."/images/";
$open = opendir($dir);
while(($file = readdir($open)) != FALSE){
if($file!="."&&$file!=".."&&$file!="Thumbs.db"){
echo "<img border='1' width='70' height='70' src='$dir/$file'>";
}
}
echo " <b>".$_SESSION['name']."</b>'s session<br /><a href='logout.php'>Logout</a>";
if($user_level == 1){
header("Location: links.php");
}
else($user_level == 2){
header("Location: client.php");
}
?>
Your syntax is wrong and you should not redirect if you want to show links, it should probably be something like:
if($user_level == 1){
// don't redirect here
include "links.php";
}
// check the php manual for the correct usage of if / else / elseif
elseif ($user_level == 2){
header("Location: client.php");
// terminate the script
exit;
}
Also, when using header("Location: client.php"); be sure that this is the first thing that comes out of your script. You can't dump out a whole html page and then put this at the bottom, it won't redirect.
How do I redirect to a splash page once with cookies?
I'm setting the cookie on my splash.php page to this:
<?php
$expire = time()+60;
setcookie("no_splash", "1", $expire);
?>
On that page there's a link to my index.php with this:
<?php
if($_COOKIE['no_splash']== '1') {
header("Location: index.php");
echo "it works";
} else if($_COOKIE['no_splash']!= '1') {
header("Location: splash.php");
};
?>
I keep getting a redirect loop error but can't figure why.
You are redirecting to index.php from the index.php file, hence the loop.
Change your code to be simply
if($_COOKIE['no_splash'] != '1') {
header("Location: splash.php");
exit;
}
or indeed
if(!$_COOKIE['no_splash']) {
header("Location: splash.php");
exit;
}
which is the same thing.
$expire = time()+60;
header("Set-Cookie: no_splash=1; expires=$expire; path=/");
header("Location: index.php");
Have you tried simply:
<?php
if($_COOKIE['no_splash']== '1') {
echo "it works";
} else {
header("Location: splash.php");
};
?>
Maybe isset($_COOKIE['no_splash']) rather than `$_COOKIE['no_splash']== '1'?
Also, not sure it this is what you want, but you can set simply not set the expiration time (or set it to 0), and it will delete the cookie when the browser is closed, so if they keep it open, they won't have to go back to the splash.
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.
I don't know what is wrong with this php script but it makes the remember log-in for the device iPhone stop working (will log out every time when refresh the page) but the other device such as iPad, and all the PCs still can use remember me function still.
<?php
if(!isset($_GET['link'])){
$link = 1;
} else {
$link = $_GET['link'];
}
if ($link == 1) {
echo "";
} elseif ($link == 23) {
echo "";
} else {
echo "";
}
?>
so every single time the device refresh it will redirect to the login page again
but then if I remove this script then it will remember the login
any help will be appreciated
Use session_set_cookie_params on the header:
session_set_cookie_params(2*7*24*60*60);