473,396 Members | 1,871 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,396 software developers and data experts.

Redirection if a condition is met

I know this topic has been discussed a few times here and also on
php.net, but I cannot find an answer to my specific problem. I check to
see if a user is logged in before serving admin.php like so:
<?php
session_start();
if (!isset($_SERVER['username']) or !isset($_SERVER['password'])) {
header("location:http://linux-place.com");
exit;
}
?>
These are the very first lines in the file. It works perfectly. The
problem is that I would like to output a message like "Please login
first before accessing the administration panel". How can I do that
without getting the headers already sent error?

Thanks,
Ahmed El-Daly

Jul 28 '05 #1
3 1711


ae*****@gmail.com wrote:
I know this topic has been discussed a few times here and also on
php.net, but I cannot find an answer to my specific problem. I check to
see if a user is logged in before serving admin.php like so:
<?php
session_start();
if (!isset($_SERVER['username']) or !isset($_SERVER['password'])) {
header("location:http://linux-place.com");
exit;
}
?>
These are the very first lines in the file. It works perfectly. The
problem is that I would like to output a message like "Please login
first before accessing the administration panel". How can I do that
without getting the headers already sent error?

Thanks,
Ahmed El-Daly


i generally use a 3 step procedure:

1. check if headers are sent, if not use header to redirect

2. if headers are sent already, print a javascript relocation routine

3. as a fallback if javascript is disabled, print a normal link

micha

Jul 28 '05 #2
Is http://linux-place.com the URL of the login page?
If it is, why not passing the message to this URL (or a message key):

<?php
session_start();
if (!isset($_SERVER['username']) or !isset($_SERVER['password'])) {

header("location:http://linux-place.com?messageKey=loginBeforeAdminPanel");
exit;
}
?>

and, in the login PHP page :

<?php
$message = getMessageFromKey($_GET["messageKey"]);
// display the message
// display the login form
?>

You should even pass the URL of the page requested before the redirect
to the login page, and redirect to this URL once the login is
successful. (Or put this URL in the session before the redirect to the
login page, and redirect to this URL after the login is successful).

JB.

Jul 28 '05 #3
YES!!! Thanks JB. That is exactly what I am going to do.

Jul 28 '05 #4

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

2
by: Albert Ahtenberg | last post by:
Hello, I don't know if it is only me but I was sure that header("Location:url") redirects the browser instantly to URL, or at least stops the execution of the code. But appearantely it continues...
3
by: Wazz Up | last post by:
Hi, I'm trying run an image slide show where the images rotate once each, then after the last image in the array has been displayed for the specified amount of time, a redirection to another...
52
by: Gerard M Foley | last post by:
Can one write a webpage which is not displayed but which simply redirects the user to another page without any action by the user? Sorry if this is simple, but I am sometimes simple myself. ...
15
by: Taki Jeden | last post by:
Hello everybody Does anybody know why w3c validator can not get pages that use 404 htaccess redirection? I set up two web sites so that clients request non-existent urls, but htaccess redirects...
7
by: Steph | last post by:
Bonjour, Je souhaite lancer une redirection vers un fichier php via SRC= dans une condition if (voir ci-dessous en bas du script) mais la redirection ne fonctionne pas. Par contre la condition...
3
by: domtam | last post by:
Hi there. My goal is to add some script to my home page (which is NOT asp / aspx page, and it is not hosted in IIS) so that any request from mobile browser will be redirect to another page (a...
8
by: Luciano A. Ferrer | last post by:
Hi! I was following the http://www.seomoz.org/articles/301-redirects.php article, trying to do that with one of my test sites I added this to the .htaccess file: RewriteEngine On RewriteCond...
13
by: souissipro | last post by:
Hi, I have written a C program that does some of the functionalities mentionned in my previous topic posted some days ago. This shell should: 1- execute input commands from standard input,...
1
by: comp.lang.php | last post by:
require_once("/users/ppowell/web/php_global_vars.php"); if ($_GET) { // INITIALIZE VARS $fileID = @fopen("$userPath/xml/redirect.xml", 'r'); $stuff = @fread($fileID,...
13
by: Massimo Fabbri | last post by:
Maybe it's a little OT, but I'll give it try anyway.... I was asked to maintain and further develop an already existing small company's web site. I know the golden rule of "eternal" URIs, but...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.