I am trying to detect if a cookie named "authenticated" has a value of "true" if so redirect.
Here is what I have so far:
$named_cookie= "authenticated";
if($_COOKIE[$named_cookie] === "true"){
flush();
header( 'Location: https://app.website.com' );
exit (); }
For whatever reason it works in chrome but not in FF or IE
Try:
<?php
$named_cookie= "authenticated";
if(isset($_COOKIE[$named_cookie]) && $_COOKIE[$named_cookie] == "true"){
header('Location: https://app.website.com');
exit(); }
?>
;)
I figured it out. I am hosting my site on WPEngine and they dont allow cookie access with PHP it all has to be dont via AJAX and JS.
Related
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');
}
?>
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.
<?php
ob_start();
echo "<body><p>Hello "
if ($condition) {
header( "Location: http://www.google.com/" );
exit;
}
echo " World!</p></body>";
ob_end_flush();
?>
When $condition is true I get this:
<body>Hello
What I want is when $condition will be true then go to Google!!!
I don't know what is happening, can you explain or give me a solution!?
Thanks.
Just add ob_end_clean(); before the header call.
Everything should work, just put an ; after echo "<body><p>Hello" and you will be fine..
If I were you, I would have started what might go wrong first then do the processing.
An example
$exit_condition_1 = some_value1;
$exit_condition_2 = some_value2;
if($exit_condition_1 == false){
//Redirect
//Exit
}
if(!$exit_condition_2){
//Redirect
//Exit
}
//start the buffer ob_start()
//show some HTML
//flash the buffer ob_end_clean()
there is no point of starting the buffer then if something goes wrong close it and redirect. Just do value testing at the begining then process the request.
An example: lets say that you want to view a product's info and you have a function that will do that
function view_product($product_id){
if(!$product = getProductById($product_id)){
//product does not exist, redirect
}
if(the user does not have enough access rights){
//show a message maybe
//redirect
}
//everything is alright then show the product info
}
To resolve a similar situation where a function was using ob_start() and there was header("Location: http://www.example.com"); after that but erring "already sent...", I replaced the header(... call with
echo "<script> window.location.href = 'https://www.example.com' </script>"
and it worked in that particular case (all that was needed was a just page redirect anyway).
This if statement has been deprecated
if(!session_is_registered('firstname')){
header("location: index.php"); // << makes the script send them to any page we set
} else {
print "<h2>Could not log you out, sorry the system encountered an error.</h2>";
exit();
}
I replaced it with
if ( isset( $_SESSION['firstname'] ) ){
header("location: index.php"); // << makes the script send them to any page we set
} else {
print "<h2>Could not log you out, sorry the system encountered an error.</h2>";
exit();
the initial code is attached to my logout.php script. When i then go to the link logout.php, this is displayed "Could not log you out, sorry the system encountered an error."
Is that the right solution since i had no problem with the code
Based on your first snippet, I think you are failing in logic.
Try:
if ( !isset($_SESSION['firstname']) ){
header("location: index.php"); // << makes the script send them to any page we set
} else {
exit('<h2>Could not log you out, sorry the system encountered an error.</h2>');
}
Do you have session_start(); on your page?
http://php.net/manual/en/function.session-start.php
hi i just dont understand why my code is not working. i am using yahoo server for my site.
This is my logout code.(which is successfully run on localhost) but when i upload this code online then its not work. plz help
<?php
//logout code
include("../Config.php");
if (!isset ($_SESSION['username']))
{
header( 'HTTP/1.1 301 Moved Permanently' );
header('Location: ../index.php');
if (!headers_sent())
{
header('Location: http://www.mysite.com/index.php');
exit;
}
}
else
{
$_SESSION = array();
session_destroy();
session_unset();
header( 'HTTP/1.1 301 Moved Permanently' );
header('Location: ../index.php');
if (!headers_sent())
{
header('Location: http://www.mysite.com/index.php');
exit;
}
}
?>
the config.php file includes session code (like start session)
You need to use the full URI in the header, and I recommend to use exit() right after the location header. There is no need for the 301 header for a simple log out.
And don't use the closing tag in php. If it is working on your system, it looks, there is some output (maybe just an empty line) in at least one of your php files (before the starting php tag, or after the closing php tag), and it seems that output buffering is enabled in your PHP, which work around this error, but disabled on the production server.
Try this:
<?php
// for debugging purposes only, don't use on production server (just for debugging)
error_reporting(E_ALL);
ini_set('display_errors', 1);
//logout code
include("../Config.php");
if (isset($_SESSION['username']))
session_destroy();
header('Location: http://www.mysite.com/index.php');
exit;
echo '<script type="text/javascript">
function delayer(){
window.location = "../index.php"
}
setTimeout("delayer()", 1000);
</script>';
You could put this instead of header
This will work
<script type="text/javascript">
window.location="http://www.newlocation.com";
</script>