page not redirecting - php

session_start();
$_SESSION['fname']=$row['fname'];
$_SESSION['user']=$row['name'];
$_SESSION['adpoint'] = $row['adpoint'];
$_SESSION['phone']=$row['phone'];
$_SESSION['id'] = $row['id'];
$_SESSION['rememberMe'] = $_POST['rememberMe'];
// Store some data in the session
setcookie('smsapp',$_POST['rememberMe']);
echo "your name"." ".$_SESSION['user'];
Print_r ($_SESSION);
header("Location: http://www.niktrixhosting.com/login/user/index.php");
here in this code header("Location: http://www.niktrixhosting.com/login/user/index.php");
this line is not redirecting it
plz mentionif any another function for redirecting page .

You can't echo and print_r or give any other output before function header.

You're trying to set the headers after sending them. Comment out the echo "your name"." ".$_SESSION['user']; line and everything will work.
Explanation: The headers are always the first thing sent (that's why they're called headers). When you call echo, part of the page is sent, but if the headers aren't set, they will be sent first. What's happening in your code is that the echo line is sending the headers, and then you're trying to set them after the headers have already been sent (which doesn't work).

Headers should be sent before any output, so any statement with an echo or a print_r before a header will cause errors.

try
echo "<script>window.location=\"http://www.niktrixhosting.com/login/user/index.php\"</script>";
instead of:
header("Location: http://www.niktrixhosting.com/login/user/index.php");

you are not to echo/print out/output anything before using header(). that will really affect your PHP page.

Related

PHP Should I use ob_clean after ob_start

I made a simple login-system in php and mysql, but I keep getting errors saying that headers already been sent, and using ob_start fixes this problem, but im not sure if I should then use ob_clean at the footer afterward?
Also, the error comes when I have logged in to the account page, saying header already been sent in previuos page - > header("Location: account.php"); But I have to redirect the user when they login.
My login page looks like this
require_once('models/init.php'); // db connection and other functions
include('header.php'); // some html code for the header, with one line php-function to check if user is logged in, if so show "home" tab instead of "login"
{
php code to check if username/pass matches etc, and if so redirect to account page
header("Location: account.php");
}
echo "<form>" // display the login form
include("footer"); // including footer, some html/js code.
This code above works if I use ob_start in the header.php file. But should I use ob_clean afterwards in the footer.php file?
Sorry if anything is unclear, english is not my first languish
Thanks!
The general principle is you cannot use echo before header(). So, this will never work:
echo "this is my header";
header("Location: account.php");
echo "this is my footer";
However, if you sent the headers first, everything works fine:
header("Location: account.php");
echo "this is my header";
echo "this is my footer";
In your case, you should do the check before you include the header:
require_once('models/init.php'); // db connection and other functions
if ($user_is_logged_in) { // Do your check here
header("Location: account.php");
}
include('header.php'); // some html code for the header, with one line php-function to check if user is logged in, if so show "home" tab instead of "login"
echo "<form>" // display the login form
include("footer"); // including footer, some html/js code.
ob_start() captures the output (what would otherwise be printed or echoed). If you don't need to echo the output or to do anything with it then just use ob_end_flush() when you are finished.

Create a cookie with PHP

I don't understand where is my mistake:
Page a.php:
setcookie("user",$username,time()+300);
Page b.php:
echo $_COOKIE['user']; or echo $_COOKIE["user"];
The result is that echo doesn't print nothing!
Thank you very much for the help!
Either $username has no value or you have white-space before you are setting the cookie. Cookies must be sent with the header information before any HTML is sent at all so make sure the

Alternative to "header" for re-directs in PHP

I am working on a project and I am required to run my program on someone else's webserver. It is a pretty simple login page that I am having the problem with. The program works correctly if I run it through my local host via WAMP. The problem I am having is that the re-direct portion is not working correctly it validates the user and starts a session but when it gets to the redirect nothing happens.
I am either doing something wrong with my syntax which I think is highly unlikely since it does work correctly through my local host. Or alternatively I'm thinking that the server does not have that function (not sure if its possible to pick and choose which modules your server supports although I'm sure it's feasible).
I don't know if it matters but they are using "cpanel" which is where I can access the files and there all in the same directory so if someone could tell me where I am going wrong or suggest an alternative to redirecting via "header" any help would be greatly appreciated. I've looked around but it seems that "header" is the standard work horse.
Heres the code I have:
if( (!empty($_POST['username'])) && (!empty($_POST['password'])) )
{
// username and password sent from Form
$myusername = $_POST['username'];
$mypassword = $_POST['password'];
$sql="SELECT UserName FROM User WHERE UserName='$myusername' and Password='$mypassword'";
$result=mysql_query($sql);
$row=mysql_fetch_array($result);
//$active=$row['active'];
$count=mysql_num_rows($result);
// If result matched $myusername and $mypassword, table row must be 1 row
if($count==1)
{
echo "we made if to the if";
session_start();
session_register("myusername");
$_SESSION['login_user']=$myusername;
echo "right b4 the re-direct";
header("location: UI.php");
exit;
}
else
echo "Your user name/password was not correct pleast TRY AGAIN!!!";
}
Update: In response to the statements about the echos would the problem possible by that I am processing my form in the same file and using echo $_SERVER['PHP_SELF']
I use this function for redirect...
Which works in all situations..even if headers are already sent..or even javascript is disabled..
function redirect($url)
{
if (!headers_sent())
{
header('Location: '.$url);
exit;
}
else
{
echo '<script type="text/javascript">';
echo 'window.location.href="'.$url.'";';
echo '</script>';
echo '<noscript>';
echo '<meta http-equiv="refresh" content="0;url='.$url.'" />';
echo '</noscript>'; exit;
}
}
By using the below code we redirect the page
$page = $_SERVER['REQUEST_URI'];
echo '<script type="text/javascript">';
echo 'window.location.href="'.$page.'";';
echo '</script>';
From the docs:
Remember that header() must be called before any actual output is sent, either by normal HTML tags, blank lines in a file, or from PHP.
Your echo is causing the redirect to fail, as is any other output sent before the header.
echo "<script>window.location.href='yourPage.php'</script>";
Redirecting via headers is illegal if any content has come through. Take out all the echos and it should work. If you do want there to be a message shown to the user before the redirect, you'll likely have to do it with JavaScript via location.href.
You cannot set headers after outputting some text, so either you move the header call before the echo statements (fine in your case) or you use output buffering.
You probably have already sent content to the browser before the php header is sent.
This can be just a \n after a closing php tag ( ?> )
To find the place where unwanted output is generated canbe hard on bigger projects or long lines of classes extending each other.
To find the problem you can do this:
error_reporting(E_ALL);
ini_set("display_errors", 1);
header("Location: https://mydomain.com/myLoginAdress/");
die();
Strict error reporting will throw line and file of the output.
Use This Code
Syntax :
echo '<script type="text/javascript"> window.location="<Your_URL>";</script>';
write this line insted of header("location: UI.php");
echo '<script type="text/javascript"> window.location="UI.php";</script>';

why doesn't my php redirect work?

Im writing a login script. My db.php doesnt echo/print anything so why doesnt header("Location: index.php"); redirect upon successful login? the login info is correct. I know i need to sanitize the input but that is not a problem at the moment.
<?php
require('db.php');
$username = $_POST['un'];
$password = $_POST['pw'];
$qry="SELECT uid FROM users WHERE name='$username' AND pass='".md5($password)."'";
$result=mysql_query($qry);
if(mysql_num_rows($result) == 1){
session_regenerate_id();
$user = mysql_fetch_assoc($result);
$_SESSION['S_UID'] = $user['uid'];
session_write_close();
header("Location: index.php");
exit();
}else{
echo "<center><form action='index.php' name='login'>Login failed! Please try again with correct username and password.<br><input type='submit' name='failed' value='Return to Login'></form></center>";
exit();
}
?>
the function header will only work if no output has been sent by the script before the function is called. Check if any codes above has echoed something.
If not, check that the include files above do not have an extra space or newline after the closing "?>" tag. Otherwise this space or newline will be sent to the browser before your header.
Are you sure no output is being produced anywhere in the script (or db.php)? Not even a few extraneous spaces ?
Try using the full URL for example:
header("Location: http://www.mysite.com/index.php");
Some browsers (thanks IE) doesn't understand the 301 redirect code with uncompleted URI.
This header location behavior in PHP is a good tool, but easy to be implemented in the incorrect scenario...for this purpose i would consider using require() instead of header(location)
like:
if (!imLoggedIn()){
require('loginForm.php');
exit;
}

Do all header("Location: member.php?id=$username") have to be the first thing in a script? (PHP)

I know in the Manuel it says that the header has to be the first thing in a script, but how come I see some codes where header("Location: member.php?id=$username") is in a if-statement?
Ex:
//a bunch of codes above
if($result!="0"){
// authenication correct lets login
$_SESSION["password"] = $password;;
$_SESSION["username"] = $username;
header("Location: member.php?id=$username");
}
else
{
echo "Wrong username or password. Please try again!";
}
But when I do this, it sometimes would/won't throw an error.
How do I allow the header (); to be used in a script without any errors?
I want to redirect the user back to the login if they click "no" and to the homepage if they click "yes".
It doesn't have to be the first thing in the script. But it haves to be the first thing that you output to the user. You MUST NOT echo stuff before using the header function. If you don't, you can use it at any place you want.
You could also "ignore output" using ob_start and ob_end_clean.
Best regards,
T.
You have to use output buffering so that nothing is sent back to the browser until the entire operation is complete. Check out the ob_start function, that should give you a good starting place.
ob_start
Without output buffering, the script sends header info back to the browser, and once that has happened you cannot use header() to redirect.
header('Location: member.php?id='.$username);
You might want to change and use this code instead if you are just going to send or $_GET['username'] an value from your previous session.

Categories