I am trying to set a session variable from page to another. It was working on my localhost but then I uploaded it to my server and now it fails. I have done some basic bug tests, and it will print at specific points, I have labeled them in the code. Here is the code the code I am using
//PAGE 1
session_start();
if(isset($_REQUEST['id'])){
$_SESSION['id'] = $_REQUEST['id'];
//prints here just fine then fails after
header('location: app.php');
}elseif($_POST){
if(isset($_SESSION['error'])){
unset($_SESSION['error']);
}
$questions = array();
for($i = 1; $i <= 11; $i++){
$questions['q'.$i] = $_POST['q'.$i];
}
$check = "";
foreach($questions as $key => $val){
$check .= $val;
}
$metreq = strcmp($check, "1111-1-1-1-11-1-1");
if(!$metreq){
$_SESSION['questionaire'] = $questions;
header("location: app.php");
}else{
header('location: nq.php');
}
}
///PAGE 2
<?php
error_reporting(E_ALL); ini_set('display_errors', 1);
session_start();
require_once('inc/state_list_options.php');
if(isset($_SESSION['questionaire']) || isset($_SESSION['id'])){
$id = $first_name = $middle_name = $last_name = $suffix = $address = $city = $state = $zip = $phone_area = $phone_exch =
$phone_num = $phone2_area = $phone2_exch = $phone2_num = $email_address = $birth_month = $birth_day = $birth_year =
$job = $referred = "";
$selected = "selected";
$questions = array();
//here we will generate a session id that is the code passed from the link clicked by the user
$conn = mysql_connect('host', 'user', 'password');
mysql_select_db('db', $conn);
if($conn){
if(isset($_SESSION['id'])){
$id = $_SESSION['id'];
$q = "SELECT * FROM eagle.pre_application WHERE id = '$id'";
$r = mysql_query($q, $conn);
$row = mysql_fetch_assoc($r);
$first_name = $row['first_name'];
$middle_name = $row['middle_name'];
$last_name = $row['last_name'];
$suffix = $row['suffix'];
$address = $row['address'];
$city = $row['city'];
$state = $row['state'];
$zip = $row['zip'];
$phone_area = $row['phone_area'];
$phone_exch = $row['phone_exch'];
$phone_num = $row['phone_num'];
$phone2_area = $row['phone2_area'];
$phone2_exch = $row['phone2_exch'];
$phone2_num = $row['phone2_num'];
$email_address = $row['email_address'];
$birth_month = $row['birth_month'];
$birth_day = $row['birth_day'];
$birth_year = $row['birth_year'];
$questions['q1'] = $row['q1'];
$questions['q2'] = $row['q2'];
$questions['q3'] = $row['q3'];
$questions['q4'] = $row['q4'];
$questions['q5'] = $row['q5'];
$questions['q6'] = $row['q6'];
$questions['q7'] = $row['q7'];
$questions['q8'] = $row['q8'];
$questions['q9'] = $row['q9'];
$questions['q10'] = $row['q10'];
$questions['q11'] = $row['q11'];
$_SESSION['questionaire'] = $questions;
}
$q2 = "SELECT job_posting.id, job_posting.title, location.city, location.state
FROM eagle.job_posting
LEFT JOIN eagle.location
ON job_posting.location__Id = location.id";
$j = mysql_query($q2, $conn);
?>
<!-- job application goes ehre -->
<?
}else{
$_SESSION['error'] = "Please fill out the questionaire before completing the applicationn.";
header('location: index.php');
exit;
}
}else{
echo "There is an error";
}
?>
I'm not 100% sure where to go from this point forward. I was thinking that it was the headers causing and issue, but that has never appeared so at this poitn I am baffled. Please let me know what you think about this. If you need additional code or further examples please let me know and I can provide them for you. Thank you in advance to anyone who stops by to help!
EDITS/DEVELOPMENTS/UPDATES
9:07 AM: Using developer's tools on chrome it is now receiving a 500 server error when submitting to page 2
9:16 AM Removed errors found with the require_once(''); There was an error in the path. Now it just automatically goes to the redirect on page 2 even if a variable is manually passed in the header via the address bar. I'm stumped still. code updated to reflect changes
Related
Kindly pardon me if i look silly mates.
My issue is , i have a profile page which on update and submit changes the data in db. To refresh the data then, i use select query after that. Then i save it in the current session. still the changes happens only in the page where the update and select query presents . I have some pages which is been included like top bar, left nav . which changes only if i refresh the page . My code is as follows
<?php
if(isset($_POST['update'])) {
$name_t = $_POST['name'];
$email_t = $_POST['email'];
$pass_t = $_POST['password'];
$contact_t = $_POST['contact'];
$address_t = $_POST['address'];
$dob_t = $_POST['dob'];
$religion_t = $_POST['religion'];
$pic_t = ($_FILES['imagefile']['name']);
$sql = "
UPDATE teacher
SET t_name = '$name_t'
, t_email = '$email_t'
, t_password = '$pass_t'
, t_phone = '$contact_t'
, t_address = '$address_t'
, t_dob = '$dob_t'
, t_religion = '$religion_t'
where teacher_id='$update_id'
";
$retval = mysql_query($sql,$link);
if(! $retval ) {
die('Could not update data: ' . mysql_error());
}
if (!empty($_FILES['imagefile']['name'])) {
$sql = "UPDATE teacher SET t_photo = '$pic_t' where teacher_id='$update_id' ";
$retval = mysql_query($sql,$link);
$info = pathinfo($_FILES['imagefile']['name']);
$ext = $info['extension']; // get the extension of the file
$target = 'img/upload/'.$pic_t;
move_uploaded_file( $_FILES['imagefile']['tmp_name'], $target);
}
}
$result = mysql_query("SELECT * FROM teacher where teacher_id='$update_id' ",$link);
while($row = mysql_fetch_array($result)){
$name = $row['t_name'];
$email = $row['t_email'];
$password = $row['t_password'];
$contact = $row['t_phone'];
$address = $row['t_address'];
$dob = $row['t_dob'];
$religion = $row['t_religion'];
$img = WEB_URL . 'img/upload/'.$row['t_photo'];
$_SESSION['objLogin'] = $row;
}
mysql_close($link);
?>
Kindly help me in updating the included page too without refresh . Because for instance there is the username displayed at top bar .
I have a profile page on my website where can you login and update your account details, such as: First Name, Last Name, Username, Company, Address Line 1 etc.
See code:
if ($result->num_rows > 0) {
while($row = $result->fetch_assoc()) {
$AccountID = $row["AccountID"];
$FName = $row["FName"];
$LName = $row["LName"];
$Username = $row["Username"];
$Company = $row["Company"];
$AddressL1 = $row["AddressL1"];
$AddressL2 = $row["AddressL2"];
$Town = $row["Town"];
$County = $row["County"];
$PostCode = $row["PostCode"];
$Password = $row["Password"];
$DFName = $row["DFName"];
$DLName = $row["DLName"];
$DAddressL1 = $row["DAddressL1"];
$DAddressL2 = $row["DAddressL2"];
$DTown = $row["DTown"];
$DCounty = $row["DCounty"];
$DPostCode = $row["DPostCode"];
}
if ($_SESSION['login_user']) {
If they are logged in then it'll echo out each value to the input fields and you can change and update them as you will...
Rather than write out
$login_session = stripslashes($login_session);
$login_session = mysql_real_escape_string($login_session);
$login_session = trim($login_session);
For every single variable, what is the best option?
You could do something such as:
function makeSafe($var)
{
$return = stripslashes($var);
$return = mysql_real_escape_string($return);
$return = trim($return);
return $return;
}
$login_session = makeSafe($login_session);
This will avoid the many lines of code on one page and avoid duplicate code
And as I said in my comment, try to avoid mysql_* functionality and replace it with either mysqli or PDO
I'm using php for a profiles site that I'm working on and I need to check that the profile id doesn't equal the member id. So I have this:
<?php
$get_personal_sql = "SELECT * FROM members WHERE username = '$thisuser'";
$get_personal_res = mysqli_query($con, $get_personal_sql);
while ($personal = mysqli_fetch_assoc($get_personal_res)){
$yourid = $personal['id'];
$yourtype = $personal['type'];
$yoursubtype = $personal['subtype'];
$yourusername = $personal['username'];
$youremail = $personal['email'];
$yourname = $personal['name'];
$youractive = $personal['active'];
$yourpro = $personal['pro'];
$yourdel = $personal['del'];
$yourdate = $personal['date'];
}
?>
<?php
$get_member_sql = "SELECT * FROM members WHERE username = '$thismember'";
$get_member_res = mysqli_query($con, $get_member_sql);
$count_member = mysqli_num_rows($get_member_res);
if($count_member == 0){
header("Location: notfound.php");
}
while ($member = mysqli_fetch_assoc($get_member_res)){
$memberid = $member['id'];
$membertype = $member['type'];
$membersubtype = $member['subtype'];
$memberusername = $member['username'];
$memberemail = $member['email'];
$membername = $member['name'];
$memberactive = $member['active'];
$memberpro = $member['pro'];
$memberdel = $member['del'];
$memberdate = $member['date'];
}
?>
<?php
if($memberid === $yourid){
$yourprofile = "true";
}
else{
$yourprofile = "false";
}
?>
I have echoed both $memberid & $yourid in the page and they are Completely different, yet when I echo $yourprofile it displays "true". Can anyone see why this might be?
I'm building a website to learn PHP and have just built a membership app.
Here's my code to get the user cookies that I set when the user logs in, then take the business id they are associated with, called biz and look up all the details for the business with the id equal to biz, in the table named company: (btw, I know I'm using mysql but when I finalize my app, I'll switch to PDO or mysqli)
<?
$auth = $_COOKIE["auth"];
if ($auth != "1"){
header("Location: ./signin.php");
}
//Grab all the cookies
$firstname = $_COOKIE['firstname'];
$id = $_COOKIE['id'];
$fname = ucwords($_COOKIE['firstname']);
$lname = ucwords($_COOKIE['lastname']);
$email = $_COOKIE['email'];
$city = ucwords($_COOKIE['city']);
$biz = $_COOKIE['biz'];
if(!empty($biz)){
$donthaveabizyet = "false";
}
else{
include("./config.php");
$result = mysql_query("SELECT * FROM company WHERE id = '$biz'") or mysql_error();
while($row = mysql_fetch_array($result))
{
$business_name = $row['name'];
$business_phone = $row['phone'];
$business_website = $row['website'];
$business_phone = $row['phone'];
$business_cat1 = $row['cat1'];
$business_cat2 = $row['cat2'];
$business_cat3 = $row['cat3'];
$business_subcat1 = $row['subcat1'];
$business_subcat2 = $row['subcat2'];
$business_subcat3 = $row['subcat3'];
$business_email = $row['email'];
$business_product1 = $row['product1'];
$business_product2 = $row['product2'];
$business_product3 = $row['product3'];
$business_product4 = $row['product4'];
$business_product5 = $row['product5'];
$business_product6 = $row['product6'];
$business_product7 = $row['product7'];
$business_noaddress = $row['noaddress'];
$business_address = $row['address'];
$business_address2 = $row['address2'];
$business_zipcode = $row['zipcode'];
$business_city = $row['city'];
}
$result = mysql_query("SELECT * FROM company_secondary WHERE company_id = '$biz'") or mysql_error();
while($row = mysql_fetch_array($result))
{
$business_description = $row['company_description'];
$business_since = $row['phone'];
$business_logo = $row['logo'];
$business_since = $row['since'];
$business_smoking = $row['smoking'];
$business_delivery = $row['delivery'];
$business_alcohol = $row['alcohol'];
$business_kids = $row['kids'];
$business_wheelchair = $row['wheelchair'];
$business_twitter = $row['twitter'];
$business_facebook = $row['facebook'];
$business_youtube = $row['youtube'];
$business_creditcards = $row['creditcards'];
$business_outdoor = $row['outdoor'];
$business_featured = $row['featured'];
}
}
?>
Now I show a link to claim.php if the user's business id is equal to 0, or if the user's business id is set, I show the name of the business.
<?php
if($donthaveabizyet != "false")
{
echo "<br/><br/>You haven't claimed a business yet. <a href='claim.php'>Click here to claim one now.</a>";
}
else
{
echo $business_name;
}
?>
Unfortunately, $business_name isn't displaying, and the error is Notice: Undefined variable: business_name. Why is business_name not set?
Big thanks for all help!!
while($row = mysql_fetch_array($result))
{
is causing your problem. Change it to
while($row = mysql_fetch_assoc($result))
{
This is because fetch_array creates an array with numeric indexes ($array[1], $array[2], etc.). fetch_assoc makes the indexes the same as the column names ($array['this'], $array['that'], etc.)
I am trying to edit this code so that instead of just inserting the info - it checks to see if the file already exists in the database, if it does it inserts as it does now. If it does exist... it should just update the info, adding the "value" amount to the value amount already in there instead of replacing it. But this is very new to me and I am lost so any help with really be appreciated!
<?php
define('JPATH_BASE',$_SERVER['DOCUMENT_ROOT']);
require_once($_SERVER['DOCUMENT_ROOT']."/b2/configuration.php");
require_once($_SERVER['DOCUMENT_ROOT']."/b2/libraries/joomla/factory.php");
require_once($_SERVER['DOCUMENT_ROOT']."/b2/libraries/joomla/base/object.php");
require_once($_SERVER['DOCUMENT_ROOT']."/b2/libraries/joomla/database/database.php");
require_once($_SERVER['DOCUMENT_ROOT']."/b2/libraries/joomla/database/database/mysql.php");
$config = new JConfig;
$options = array();
$options['host'] = $config->host;
$options['user'] = $config->user;
$options['password'] = $config->password;
$options['database'] = $config->db;
$options['prefix'] = $config->dbprefix;
$options['select'] = true;
$db = new JDatabaseMySQL($options);
//$user = JFactory::getUser();
$userId = 0;
//($user->id > 0) ? $user->id : 0;
$numbervote = $_REQUEST['w'];
$folder = $_REQUEST['w2'];
$name = $_REQUEST['w1'];
$date = date('Y-m-d H-i-s');
$sql = "INSERT INTO jos_image_ratetting (id,userid,datecreated,value,filename,folder,md5name) VALUES(NULL,'".$userId."','".$date."','".$numbervote."','".$name."','".$folder."',MD5('".$name."'))";
$db->setQuery($sql);
if($db->query()){
$msg = "Thanks for rate!";
}else{
$msg = mysql_error();
}
echo $msg;
//echo 'Hello';
?>
Take a look at the ON DUPLICATE KEY UPDATE syntax for MySQL