I have a really nasty problem with sessions and includes in PHP. I have a page called index.php in which I have a switch function between the different pages:
<?php $p=(isset($_GET["p"])?$p=intval($_GET["p"]):$p=1); switch($p) {
case 1: $pagina = "login_and_registration.php";
break;
case 2: $pagina = "admin/registrazione.php";
break;
case 3: $pagina = "admin/login_test.php";
break;
case 4: $pagina = "logged_gym.php";
break;
case 5: $pagina = "logged_trainer.php";
break;
case 7: $pagina = "logout.php";
break;
case 8: $pagina = "admin/completa_profilo_trainer_sql.php";
break;
case 10: $pagina = "error.php";
break;
case 12: $pagina = "profilo_min.php";
break;
case 13: $pagina = "splash.php";
break;
default: $pagina = "index.php";
break;} ?>
Then down the code in the body of the file i have some includes:
<header class="container_12">
<?php include("login_errors.php")?>
<div id="logo_container" class="grid_4 push_4"></div></header >
And this one:
<section>
<?php include_once($pagina);?>
</section>
When I fill the form which is in this page, corresponding to page 1 in the switch function, i get this error on the login (where I start a new session with a name):
Warning: session_start() [function.session-start]: Cannot send session cookie - headers already sent by (output started at /Applications/MAMP/htdocs/FitnessOptimize/index.php:63) in /Applications/MAMP/htdocs/FitnessOptimize/admin/login_test.php on line 73
In the index.php on line 63 i have the first include that I just posted you. Any ideas on how to avoid this bug? Also my header ("Location: "); don't work i think for the same reason. Any help will be appreciated! Thanks to you all!
No output before sending headers!
the header error is caused if you have typed anything before header ,even white spave before the <?php will cause the error
for more information check the Headers already sent by PHP question on the stackoverfow
check this link to Having two different sessions in same domain
use session_start(); function at the top of the page on which you are using $_SESSION
.Be ensure that you donot have a single space before session_start()
in php first of all all the header transfers and than the text is sent to browser if there is a single space is present your session will not be started and you cannot use $_SESSION[] so write this at the top of the page
session_start();
should be at the beginning of the page
The problem may be caused by unprintable characters at the start of code, or you're writing some value in header.
Related
My website is working with 2 languages.
I used session for that purpose. When a user clicks on a language icon the language is changed and everything is fine, but when the website is opening for the first time without choosing the language the default language is not loaded and the words do not appear.
This is my language.inc.php file
<?php
session_start();
$langForSelection = isset($_SESSION['lang']) ? $_SESSION['lang'] : 'en'; //default to english language
if(isset($_GET['langSelect'])){
//allow only 2 for now ( english / turkish)
$langForSelection = "";
switch($_GET['langSelect']){
case 'en':
$langForSelection = 'en';
break;
case 'tr':
$langForSelection = 'tr';
break;
default:
break;
}
if(isset($langForSelection)){
$langForSelection2=$_SESSION['lang']=$langForSelection;
// setcookie('lang',$langForSelection,time()+24*7*60*60);//set cookie to expire in 7 days
}
}
here is the icons where user choose the language
<div><img src="images/united-kingdom.png" class= "active" style="float: right; width: 24px;height:24px ;padding: 4px">
<img src="images/turkey.png" style="float: right; width: 24px;height:24px; padding: 4px">
here I print the translate of the words
this is an example
require_once('inc/languages.inc.php');
require_once('Languages/common.inc.php');
$langForSelection2=$_SESSION['lang'];
<li><?php echo $arrLang[$langForSelection2]['log_login']; ?></li>
Again my problem is reproduced when the web page is loaded and before a user clicks on language icon the words not appear but when he click on the language click everything works fine.
You can change your switch to default to English:
switch($_GET['langSelect']){
case 'en':
$langForSelection = 'en';
break;
case 'tr':
$langForSelection = 'tr';
break;
default:
$langForSelection = 'en';
break;
}
The reason of your problem probably is that your $_SESSION has a lang value, which is different from your expectations, like 'en-us'. If you want to know the exact cause, you can reproduce the problem with a new session and calling var_dump($_SESSION); If it has a lang, then it is the exact reason of your problem.
EDIT:
The solution shown here was not enough, since this part
if(isset($langForSelection)){
$langForSelection2=$_SESSION['lang']=$langForSelection;
// setcookie('lang',$langForSelection,time()+24*7*60*60);//set cookie to expire in 7 days
}
was inside the if. You will need to move this outside the if.
I have a cms admin section with the option comments; when i press comments on the admin menu it should take me to comments.php which includes the code below; by default it should load all my comments on a grid, however when i press comments its not displaying anything the page is blank ? My code:
<?php
if(isset($_GET['source'])){
$source1=$_GET['source'];
if(!empty($source1)){
switch($source1){
case 'add_post':
include"includes/add_posts.php";
break;
case 'edit_post':
include"includes/edit_post.php";
break;
case 'view_all_comments':
include "includes/view_all_comments.php";
break;
default:
include "includes/view_all_comments.php";
}
}
}
?>
when u load comments.php the url doesnt sent any parameter via get, on your code you are checking if the source is not empty which on your case its empty at first. Then preform switch statment, in your case it will not preform the switch as the source is empty and thats why you are not seeing anything. You can fix that by adding an else on your if and include view_all_comments.php or like the code below:
<?php
$source1=isset($_GET['source']);
switch($source1){
case 'add_post':
include"includes/add_posts.php";
break;
case 'edit_post':
include"includes/edit_post.php";
break;
case 'view_all_comments':
include "includes/view_all_comments.php";
break;
default:
include "includes/view_all_comments.php";
}
?>
I have a php code that is meant to redirect people to a new page if they get the correct username and password. I have used a switch statement which has the header location function. The problem is that the header is executing for both cases and also the default keyword. I would only like the header to be excuted for one of the correct username and passwords
<?php
switch($_POST["username"] + "|" + $_POST["password"]) {
case"un1"|"pw1":
header("Location:http://MyFavWebsite.php");
break;
case"un2"|"pw2":
echo "hello";
break;
default:
echo "Go Away";
}
?>
I would like to know if this could be fixed i will appreciate any help to solve this problem.
case"un1"|"pw1":
Because that is not the right format for a string and it breaks your switch structure
case "un1|pw1":
Is.
And Oh someone might ask. That | is a bitwise OR operator and its result for your first case is TRUE that's why you always get that redirect.
header has no issue but your switch syntax is wrong, use both parameters as one condition
i.e
<?php
switch($_POST["username"].$_POST["password"]) {
case"un1pw1":
header("Location:http://MyFavWebsite.php");
break;
case"un2pw2":
echo "hello";
break;
default:
echo "Go Away";
}
//e.g
$un='un1';
$pass='pw1';
switch($un.$pass) {
case"un1pw1":
header("Location:http://MyFavWebsite.php");
break;
case"un2pw2":
echo "hello";
break;
default:
echo "Go Away";
}
?>
Try to change case"un1"|"pw1": to case "un1|pw1"
I am trying to include several different php include files in my page attach.php which display 1 include file at a time depending on which link is clicked.
Here's my links:
<div class="hewden_square"></div><h33>Insurance Documents</h33>
<div class="hewden_square"></div><h33>Policy Documents</h33>
next I have my php code which checks which link has been clicked.
<?php
switch(isset($_GET['tab'])) { //switch is just like a bunch of if()s
default: //default case
case '': //default case
include('include/attachments_overview.php'); //include page.html
break; //break, witch means stop
case 'insurance': // page2, if changePage has the value of page2
include('include/upload_insurance/upload_files.php'); //include page2.html
break; //break, witch means stop
case 'policy': // page2, if changePage has the value of page2
include('include/upload_policy/upload_files.php'); //include page2.html
break; //stop it
} //end the switch
?>
my default include file should be shown before any link is clicked and this is:
include('include/attachments_overview.php');
then one or the other should be included depending on which link is clicked.
For some reason my default include file is shown as expected and my other include file
include('include/upload_insurance/upload_files.php');
is shown when I click the link attach.php?tab=insurance
however when I click on my last link attach.php?tab=policy this just seems to show the same include file for my insurance tab
include('include/upload_insurance/upload_files.php');
can someone please show me where I am going wrong or show me a better/more reliable way of doing this? Thanks
<?php
$tb = isset($_GET['tab']) ? $_GET['tab'] : '';
switch($tb) { //switch is just like a bunch of if()s
default: //default case
include('include/attachments_overview.php'); //include page.html
break; //break, witch means stop
case 'insurance': // page2, if changePage has the value of page2
include('include/upload_insurance/upload_files.php'); //include page2.html
break; //break, witch means stop
case 'policy': // page2, if changePage has the value of page2
include('include/upload_policy/upload_files.php'); //include page2.html
break; //stop it
} //end the switch
?>
I have downloaded a backup copy of my cpanel webserver. I install WAMP on my local laptop.The database and php username and password have been set. Then I loaded the database into the local MySQL database. I verified the phpinfo.php is working. I setup dreamweaver manage site with the extracted website on my laptop. Now the back end admin pages on my laptop for my website are working. However the frontend is not completely loading and I can't figure out why.
Please go here to see what the page looks like since I can't post images on this post. dev.quicksalenetwork.com/images/frontend.png
Now if you go to the live development site you would see what is should look like.
Also here is the source code of the index.php page.
<?php
//redirect for install
if(!file_exists('Connections/myconn.php'))
header('Location:install.php');
//install file check
/*if(file_exists('install.php') || file_exists('install2.php') || file_exists('install3.php'))
print '<font color="red"><strong>Please delete install files { install.php , install1.php, install2.php }</strong></font><br>';
*/
//*******************************************************************************
//PHP Real Estate Classifieds
//All Rights reserved by Quick Sale Network inc.
//Do not modify, duplicate, resell with out direct permission from Quick Sale Network inc.
//Script is bound by terms listed in terms.html file
//*******************************************************************************
//fetch template header
include('header.php');
//define varable
define('go','');
//set index varable for switch statment
if (isset($_REQUEST['go']))
$go = $_REQUEST['go']; // requested page
else
$go = ''; // default
switch($go) {
case 'gmap':
include('gmap.php');
break;
case 'photos':
include('photos.php');
break;
case 'resource_center':
include('resource_center.php');
break;
case 'linkdir':
include('linkdir.php');
break;
case 'mailme':
include('formmail.php');
break;
case 'sell':
include('sell.php');
break;
case 'buy':
include('buy.php');
break;
case 'properties':
include('properties.php');
break;
case 'login' :
header("location: members/login.php");
break;
case 'register' :
include('register.php');
break;
case 'pricing' :
include('pricing.php');
break;
case 'calc' :
include('calc.php');
break;
case 'contact' :
include('contact.php');
break;
case 'faq' :
include('faq.php');
break;
case 'features' :
include('features.php');
break;
case 'member_login' :
include('member_login.php');
break;
case 'mailme' :
echo"<br><h4>";
include('formmail.php');
echo"</h4>";
break;
case 'detail' :
include('detail.php');
break;
case 'search' :
include('search.php');
break;
/*case 'home_gmap' :
include('home.php');*/
default:
include('home_gmap.php');
break;
}//end of switch statment
//fetch template footer
include('footer_city.php');
?>
Any ideas what I am doing wrong?
My guess:
your code uses php short open tags (<? instead of <?php) and you haven't enabled short tags in your wamp install..
There's a short_open_tag directive in your php.ini to enable it
I had the same thing happen to me. It happens when a file is corrupted. Just delete the file (Blanking it wont work) and copy all the code back into it.