<html>
<head>
<title>Location redirect test page</title>
</head>
<body>
<?php
header('Location: http://www.google.com');
?>
</body>
</html>
I uploaded this code to my server(1 and 1). Its not redirecting. I used the same code in XAMPP and it worked fine.
What am I doing wrong? Why is it working on xampp and not on real (1and1) server? I appreciate any help.
Thank you
You can not call header function after output. it shows warning as "header already sent...".
you must write it before tag.
<?php
header('Location: http://www.google.com');
?>
<html>
<head>
<title>Location redirect test page</title>
</head>
<body>
</body>
</html>
*may be your server setting for showing warning is off so it wont display warnings to you on related server.
If by some reason you can't place the php snippet before the html code (like Mayur said) you can use JavaScript for redirects
window.location.href = "http://stackoverflow.com";
Remember that when setting headers from php or setting cookies no text must be out putted before the php command!
Related
My code looks like this:
<html>
<head>
<title>PHP Test</title>
</head>
<body>
<?php echo '<p>Hello World</p>'; ?>
</body>
</html>
and outputs this:
Hello World
'; ?>
What are those extra characters doing there?
Thanks!
You can't end a file in .html and expect your web server to parse it as PHP.
In that case it's sent directly to the browser without anything being parsed or executed as PHP.
Instead, save it as index.php for example, then view it in your browser. If you have PHP installed then you should get the output you expect.
Seems like you have saved your file with .html extension instead of .php.
i have php code which was working in Mavericks, then i updated my system to Yosemite, apache 2.4.9 (unix) is installed in yosemite, now when i run my code, i got this error:
Warning: Cannot modify header information - headers already sent by (output started at /Library/WebServer/Documents/odnoklasniki/indexSingle.php:10) in /Library/WebServer/Documents/odnoklasniki/indexSingle.php on line 125
in 125 line, i have this code:
header('Location: http://www.odnoklassniki.ru/oauth/authorize?client_id='.$AUTH['application_key'].'&scope=PHOTO_CONTENT&response_type=code&redirect_uri=http://localhost/odnoklasniki/indexSingle.php');
before it was working in mavericks.
Code:
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<link href="http://www.odnoklassniki.ru/oauth/resources.do?type=css" rel="stylesheet">
</head>
<body>
<?php
if (statement true) {
**** // i have some echo's here, i tried by removing them
} else {
header('Location: http://www.odnoklassniki.ru/oauth/authorize?client_id='.$AUTH['application_key'].'&scope=PHOTO_CONTENT&response_type=code&redirect_uri=http://localhost/odnoklasniki/indexSingle.php');
}
?>
</body>
</html>
You might have used echo or html code before the header(),
Do not use/render any echo or html code before the header(), and if there any thing remove all and try, which will work.
output started at /Library/WebServer/Documents/odnoklasniki/indexSingle.php:10
see this lines and remove the echo/ blank spaces/ html etc.
You probably output something before the PHP header command, that's the cause of the error you get. You can use a die(); function before this line and call the page from the browser, you will probably see the output sent before the header() command.
I am not an expert but I am no noob at PHP, yet for whatever reason I am stomped as to why my document will not load. Here is my code.
<?php include 'header.php'; ?>
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>Home</title>
</head>
<body>
<p>Hello everyone</p>
</body>
</html>
When I pull out the PHP portion the HTML loads fine. Here is the code in my header.php file.
<?php
<href="index.php">Home</a>
?>
I have tried this on two different hosts, both of which are hosting other PHP websites and still getting issues. I have also validated it with W3Schools and another online PHP validator. Both didn't find any errors. Any help would be greatly appreciated.
Enable errors to see errors, this way:
ini_set('error_reporting', E_ALL);
ini_set('display_errors', true);
This code is a PHP error:
<?php
<href="index.php">Home</a>
?>
Try change to:
<?php
echo '<href="index.php">Home</a>';
?>
This:
<?php
<href="index.php">Home</a>
?>
Is no valid PHP. This would, however work:
Home
Inside of the PHP-tags you can only use PHP - no HTML. Also, <href> is no HTML tag.
Look at this question How to get useful error messages in PHP? to find out, how to enable error messages in PHP.
I am including a php file into JQuery, like this:
Main php file:
<?php
session_start();
?>
<script src="//ajax.googleapis.com/ajax/libs/jquery/1.9.1/jquery.min.js"></script>
<?php
$_SESSION['ID'] = "32";
?>
<div id="sesion"></div>
<script>
$("#sesion").load("fun.php");
</script>
php file that will load, fun.php:
<?php
session_start();
echo 'new php';
echo $_SESSION['ID'];
?>
fun.php shows blank as soon as I add session_start() if i remove it, it will display:"new php"
I need to open session in the "fun.php" file, but I can't seem to achieve it.
Put session_start() at the top of the page that is including chalk_element.php within it's page
Try simplifying your environment.
Create a new index.php with session_start() at the very top, then create a couple of session vars.
Next, create your chalk_element.php file and load it via jQuery in index.php.
Now do something very simple to access a session var and see what happens. This will give you a simplified test environment where you can experiment with the configuration.
It sounds like your outputting something (HTML) before the point you call session_start. You can either move session_start() so it's at the top of the page (right at the top, no whitespace) or call ob_start() first. You can then output data and call session_start() later.
Also, check your error logs for any useful error information as to why the PHP file has just stopped working.
Both files will require session_start(); at the top of each file before any other output as this function sends out several HTTP headers.
Here is a simple example:
index.php
<?php
//Start session
session_start();
//Set the value in your session
$_SESSION['key']='value';
?>
<!DOCTYPE HTML>
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8">
<title>Untitled Document</title>
<script src="http://code.jquery.com/jquery-latest.min.js" type="text/javascript"></script>
</head>
<body>
<script>
$(function() {
$("#Chalk").load('chalk_element.php');
});
</script>
<div id="Chalk">Loading...</div>
</body>
</html>
chalk_element.php
<?php
//Start session also to `resume` session
session_start();
//Check from ajax
if(isset($_SERVER['HTTP_X_REQUESTED_WITH']) && strtolower($_SERVER['HTTP_X_REQUESTED_WITH']) === 'xmlhttprequest'){
//output session var if set
echo isset($_SESSION['key']) ? $_SESSION['key'].' was set in session' : 'session value was not set';
}
?>
It was an encode problem when I saved the files. I saved both files in ANSI code and problem solved.
Try This One..!
ob_start(); //top of the page
ob_flush();//end of the page
When I create file index.php in directory /public/ my source code
<html>
<meta name="ROBOTS" content="NOARCHIVE">
<head><NOFOLLOW><noindex>
<?php echo '123' ?>
</NOINDEX></NOFOLLOW>
</head>
<body>
</body>
</html>
from browser looks like
<html>
<meta name="ROBOTS" content="NOARCHIVE">
<head><NOFOLLOW><noindex>
123
</NOINDEX></NOFOLLOW>
</head>
<body>
</body>
</html>
but if I create folder (no matter where) and place file index.php into it when source code become
<html>
<meta name="ROBOTS" content="NOARCHIVE">
<head><NOFOLLOW><noindex>
if I make some correction in this file, source code stay the same, not changed in browser view.
Why I cant create and run php code where I want, except /public/ folder?
Is that the exact code you are using in both index.php files?
If so, they should both fail as there is a syntax error. You are missing the ; at the end of the php code.
You posted: <?php echo '123' ?>
Should be : <?php echo '123'; ?>
If that´s not the case, you could add the following to the top of index.php to make sure the errors are showing on the page:
<?php
error_reporting(E_ALL);
ini_set('display_errors', '1');
?>
If any error appear after that, please post them back here.