PHP session is working on localhost but not on actual server - php

i'm having trouble to pass session variable to another page. It is working on localhost but not on server. I would want to pass $_SESSION['user_check'] to editpasswordsignup.php but whenever i click submit, $_SESSION['user_check'] is empty.
This is loginsignup.php
<?php
include('db.php');
session_start();
?>
<div id="wrapper">
<div id="wrapper-bg">
<div id="wrapper-bgtop">
<div class="container" id="header">
<div class="container" id="logo">
<h1></h1>
</div>
</div>
<div class="container" id="page">
<div id="loginbox">
<form action="" class="formbox" method="post">
<label>Email Address:</label>
<input class="box1" name="email" type="text">
<label>Password:</label>
<input class="box2" name="password" type="password">
<a class="myButton1" href="forgetpassword.php">Forgot Password</a><br>
<input class="submit5" name="submit" type="submit" value="Login">
Create Account
</form>
<?php
if (isset($_POST['submit']))
{
$email = ($_POST["email"]);
$password =($_POST["password"]);
$_SESSION['user_check'] = $_POST["email"];
$sql = mysql_query ("SELECT * FROM user WHERE email = '$email' ");
$row = mysql_fetch_array($sql);
$drawemail = $row['email'];
$drawpassword = $row['password'];
if (($drawemail == $email ) && ($drawpassword == $password ))
{
?>
<script>window.location = "../wordsignup.php";</script>;
<?php
}
else
{
echo "wrong password or username";
}
}
?>
</div>
</div>
<div class="container" id="footer-content-bg">
<div id="footer-content">
<ul>
<li class="footer1">
About us
</li>
<li>
Term and Conditions
</li>
<li>
Privacy Advertising
</li>
<li>
Policy
</li>
<li>
User Agreement
</li>
</ul><br>
<div id="copyright">
© . All Rights Reserved.
</div>
</div>
</div>
</div>
This is editpasswordsignup.php
<?php
include('db.php');
session_start();
$user_check = $_SESSION['user_check'];
?>
<div id="wrapper">
<div id="wrapper-bg">
<div id="wrapper-bgtop">
<div class="container" id="header">
<div class="container" id="logo">
<h1></h1>
</div>
</div>
<div class="container" id="page">
<div id="forgetpassword">
<div style="font-size:20px; color:#000080; font-weight:bold; border-width:1px; border-style:none; width:600px; margin:10px 0px 0px 175px;">
Edit Password
</div>
<div style="font-size:16px;border-style:none; width:560px; margin:20px 0px 10px 175px; font-weight:bold;">
Please enter your old password below and we will send you your password.
</div>
<div id="forgetpasswordbox">
<form class="forgetpassword" method="post">
Please enter your old password<br>
<input class="oldpassword" name="oldpassword" type="password"><br><br>
Please enter your new password<br>
<input class="newpassword" name="newpassword" type="password"><br><br>
Please re-enter new password<br>
<input class="confirmpassword" name="confirmpassword" type="password"><br><br>
<input name="submit" type="submit" value="Send" class="send">
</form>
</div>
</div>
</div>
<div class="container" id="footer-content-bg">
<div id="footer-content">
<ul>
<li class="footer1">
About Us
</li>
<li>
Term and Conditions
</li>
<li>
Privacy Advertising
</li>
<li>
Policy
</li>
<li>
User Agreement
</li>
</ul><br>
<div id="copyright">
© . All Rights Reserved.
</div>
</div>
</div>
</div>
</div>
</div>
<?php
if (isset($_POST['submit']))
{
$oldpassword = $_POST['oldpassword'];
$newpassword = $_POST['newpassword'];
$confirmpassword = $_POST['confirmpassword'];
$sql = mysql_query (("SELECT * FROM user WHERE email='user_check' AND password='$oldpassword' "),$conn);
$row = mysql_fetch_array($sql);
$email = $row['email'];
$selectpassword = $row['password'];
if ($oldpassword == "")
{
echo '<script language="javascript">';
echo 'alert("pls enter your oldpassword")';
echo '</script>';
exit;
}
if($newpassword=="")
{
echo '<script language="javascript">';
echo 'alert("pls enter your newpassword")';
echo '</script>';
exit;
}
if($confirmpassword=="")
{
echo '<script language="javascript">';
echo 'alert("pls enter your confrimpassword")';
echo '</script>';
exit;
}
if (($oldpassword) != ($selectpassword))
{
echo '<script language="javascript">';
echo 'alert("No user exists with this password '.$selectpassword.' ")';
echo '</script>';
exit;
}
if ($newpassword == ($confirmpassword))
{
mysql_query("UPDATE user SET password = '".$newpassword."' WHERE email='".$email."'");
$message = "Your password ".$newpassword." and click the link uploadsignup.php to upload your photo";
mail($email, "Change Password", $message);
?>
<script>
alert("Password Successfully change...!!!!\nClick OK to upload photo\nNewpassword will send to your email address");
window.location="uploadsignup.php";
</script>
<?php
}
else
{
echo '<script language="javascript">';
echo 'alert("new password does not match")';
echo '</script>';
}
}
?>

As you said it's working on localhost and not on the particular server, you might have a different settings in php.ini. Check out http://php.net/manual/en/session.configuration.php

I once had this problem for hours. My sessions worked on localhost but were not passing from page to page. The issue was the comments I had above the session starting. For some reason it didn't affect localhost but my server was just not having it.
So if there is anyone else reading this post with this issue, this is something else you can try.

There are several things that need to be corrected here:
1) Do not use styles in the page, use CSS classes and Id's rather than Styles. I have edited the question to remove these as they get in the way of the real code we're interested in.
2) Do not use MySQL, it is deprecated meaning it is no longer supported and the reason it is no longer supported is there are various flaws and security issues and I HIGHLY recommend looking into MySQLi or PDO as alternative methods of connecting to a database. Seriously.
3) Your issue is syntx - we'll start with brackets - see:
$email = ($_POST["email"]);
$password =($_POST["password"]);
Should be:
$email = $_POST['email'];
$password =$_POST['password'];
You do not need to put values in brackets when assigning variables. Also note that array values are in single quotes ' not double quotes. So:
$_SESSION['user_check'] = $_POST['email'];
Will work better, alternatively try this:
if (!empty($_POST['email'])){
$_SESSION['user_check'] = $_POST['email'];
}
else {
$_SESSION['user_check'] = "No Email value given in form";
}
to replace the above line.
4) Your HTML code is incomplete, your form has no action value in the second code block, these things are probably not critical but this code is ripe for errors due to being ambiguous.
5) At the top of editpasswordsignup.php (after session_start) put this:
var_dump($_SESSION['user_check']); and with the above code from point 3, this should give you an output.
6) Check your SQL:
$sql = mysql_query (("SELECT * FROM user WHERE email='user_check' AND password='$oldpassword' "),$conn);
Your email value is a string, not a variable. Your SQL here is also a complete mess, what is the reason for this?
replace the above with:
$sql = mysql_query("SELECT * FROM user WHERE email='$user_check' AND password='$oldpassword' LIMIT 1");
Complete all of the above and your code will work.

According to the op question and the answer needed why code is working on local host and not server the correct answer seems by Arun.
Too bad he has been down voted but that could be because he has not explained anything in his answer.
I have met this problem before.
The solution is make sure you do not have space above or below
<?php
line.
I am fairly certain that is why Arun has drawn lines to indicate your other php code should follow emediately.

I just had the same problem and I ended here. Even though I don't see there session_set_cookie_params() maybe you've typed it on db.php. Because of using LOCALHOST on session_set_cookie_params(time()+600,'/','LOCALHOST',false,true); session didn't work, so if you used session_set_cookie_params() you have to make sure you've replaced LOCALHOST with your domain

Try like this:
<?php session_start();
-----
-----
?>

Related

Login Header does not work

I'm trying to code a login-system, but I've got a problem with the login:
As you join the webpage you get to
../?p=Login
As you press the Login-button then you should be send to
../?p=index
But the header should be
../?a=loggedin
because the standard is
Includes/index.php
The PHP-code in index:
if(isset($_GET['p'])) {
$p = htmlspecialchars($_GET['p']);
} else {
$p = "index";
}
include 'Includes/' . $p . ".php";
In the Includes/index.php is a output if a equals loggedin:
if(isset($_GET['a'])) {
if($_GET['a'] == "loggedin") {
echo('<div class="Password-true"> Du hast dich erfolgreich angemeldet.
</div>');
}
}
I think the problem might be in the login code but as I don't know where
the problem is, I inserted all of the code:
<?php
if(isset($_POST['username'], $_POST['password'])) {
$username = htmlspecialchars($_POST['username']);
$password = password_hash(htmlspecialchars($_POST['password']),
PASSWORD_DEFAULT );
$login_statement = $pdo->prepare("SELECT * FROM user_users WHERE username
LIKE :username OR email LIKE :username");
$login_statement->bindParam("username", $username);
$login_statement->execute();
$user = $login_statement->fetch();
if($user != null) {
if(isset($_SESSION)) {
session_start();
}
$_SESSION['username'] = $user['username'];
header("Location: /?a=loggedin");
} else {
echo('<div class="login-false"> Benutzername und Passwort stimmen nicht
überein. </div>');
}
}
<div class="login">
<div class="login-header">
<h1>Login</h1>
<hr size="3" />
</div>
<div class="login-content">
<form method="post" action="/?p=Login">
<h3> Benutzername / E-Mail </h3>
<input type="text" class="datainput" name="username" style="height: 30px;
padding-left: 5px;" required placeholder="Nutzername oder E-Mail" /><br>
<br>
<h3> Passwort</h3>
<input type="password" class="datainput" name="password" style="height:
30px; padding-left: 5px;" required placeholder="Passwort" /><br><br><br>
<br><br>
<input type="submit" value="Login" style="height: 30px; width: 100px;" />
</form>
</div>
<div class="login-footer">
<hr size="3" />
Fülle alle Felder aus, um dich anzumelden.
</div>
</div>
Finally I want to add, that I used a tutorial on YouTube and I use Bootstrap and jQuery.
My website is: http://mysticsouls.developed-media.de
(It isn't nice yet).
Thank you for your help!
header() will not work if the headers were already sent... aka if some code/html is displayed before this part is executed.
An alternative would be to echo some JavaScript. Since you have jquery in there I thought you might be open to an alternative ;)
<script>window.location = '/?a=loggedin'</script>
From what I can see your code has some serious security issues. You should work on a local copy first, I'd even go as far as disabling the live version... For now...
It sounds like you wanting this for a url:
/?p=index&a=loggedin
Then you can $_GET both p and a from this. Otherwise, can you clarify more?

Data not update when login is successfull in index.php?

I make a login page like user login. if registered then login with email and password(navigate register.php to index.php page) or sign up if not registered. works good but i want to add update functionality in my index.php page concept is simple if user wants to update its record then he make updations in a form and submit. I make a update query here but its shows Notice: Undefined index: id in C:\wamp\www\LOginKodingMadSimple\index.php on line 34. Where i am wrong ?
index.php :
<?php
session_start();
include_once 'dbconnect.php';
?>
<?php
if (isset($_POST['UpdateUser']))
{
$name = mysqli_real_escape_string($con, $_POST['name']);
$id = mysqli_real_escape_string($con, $_POST['id']);
//------- name can contain only alpha characters and space -------
if (!preg_match("/^[a-zA-Z ]+$/",$name))
{
$error = true;
$name_error = "Name must contain only alphabets and space";
}
//------- Update users -------
if (!$error)
{
if(mysqli_query($con, "UPDATE users SET name='" . $name . "' WHERE id='" . $id . "'"))
{
$successmsg = "Successfully Updated! ";
}
else
{
$errormsg = "Error in Update...Please try again later!";
}
}
}
else
{
$errormsg = "Failed Please Try Again Later !";
}
?>
<div class="collapse navbar-collapse" id="navbar1">
<ul class="nav navbar-nav navbar-right">
<?php if (isset($_SESSION['usr_id'])) { ?>
<li>
<p class="navbar-text">Signed in as <?php echo $_SESSION['usr_name']; ?></p>
<a class="navbar-brand" href="updatephp.php">Update</a>
</li>
<li>
Log Out
</li>
<div style="border:1px dashed transparent;margin-top:400px;position:absolute;margin-left:-35%;">
<h1> Hello User <?php echo $_SESSION['usr_name']; ?> ! </h1>
<h1> Your Email is <?php echo $_SESSION['usr_email']; ?> ! </h1>
<form action="" method="post">
Name: <input type="text" name="name" placeholder="<?php echo $_SESSION['usr_name']; ?>"><br>
<input type="submit" value="Update" name="UpdateUser">
</form>
</div>
<?php } else { ?>
<li>Login</li>
<li>Sign Up</li>
<?php } ?>
</ul>
</div>
add input hidden field with id like.
<input type="hidden" name="id" value="<?php echo $_SESSION['usr_id'];?>">
[NOTE: You don't have any textbox named as id.]
Choose any way.
Way 1)
Change
$id = mysqli_real_escape_string($con, $_POST['id']);
To
$id = $_SESSION['usr_id'];
Updated Code:
<?php
if (isset($_POST['UpdateUser'])) {
$name = mysqli_real_escape_string($con, $_POST['name']);
$id = $_SESSION['usr_id'];
Way 2)
Add one hidden input field having value session usr_id.
<form action="" method="post">
Name: <input type="text" name="name" placeholder="<?php echo $_SESSION['usr_name']; ?>"><br>
<input type='hidden' value="<?php echo $_SESSION['usr_id'];?>" name='id'>
<input type="submit" value="Update" name="UpdateUser">
</form>
In this way, your submitting form code will be same as you are having.
Use below function to hide the error notice :
error_reporting(0);

My form won't work within my bootstrap

I've written code to make a login page, this worked fine. However when I implemented it into my modified bootstrap-template, it wouldn't work anymore.
edit: it will just not give any result, if I understand firebug correctly the submit button does work but the rest of it doesn't, (atleast, it gives me an output in the firebug console) it doesn't seem to send a username or password, and it does sertainly not redirect me to the page set in action=" "
Here is my code:
<div id="login" class="container" style="color:#f7f7f7; background-color:#222222">
<div class="row">
<div class="col-lg-12 text-center">
<h2 class="section-heading">Login</h2>
</div>
</div>
<div class="row text-center">
<div class="col-md-4 col-md-offset-4">
<form method="POST" action="passwordunhash.php">
<p> username: <input type="text" name="username" value="username"></p>
<p> password: <input type="password" name="password" value=""></p>
<p> <input type="submit" value="Inloggen"></p>
</form>
</div>
</div>
</div>
<?php
if (empty($_POST['username']) && empty($_POST['password']) ){
echo " ";
}
else {
$query = "SELECT rank FROM users WHERE username='$username'";
if (!$resultaat = mysql_query($query) ){
$boodschap .= "query \"$query \" mislukt";
// echo "Dit is de boodschap: ".$boodschap." einde boodschap ";
echo "username not found";
}
else {
$rij = mysql_fetch_array($resultaat);
$_SESSION['rank'] = $rij["rank"];
}
$query = "SELECT password FROM users WHERE username='$username'";
if (!$resultaat = mysql_query($query) ){
$boodschap .= "query \"$query \" mislukt";
// echo "Dit is de boodschap: ".$boodschap." einde boodschap ";
echo "username not found";
}
$rij = mysql_fetch_array($resultaat);
$hash = $rij["password"];
if (password_verify($password, $hash)){
$_SESSION['loggedin'] = true;
header("Location: /index.php");
exit();
}
else {
echo 'username or password is incorrect';
session_unset();
session_destroy();
}
}
?>
this is the part I think the problem should be in.
I tried various things, including using the bootstrap form functions and leaving the action blank.
Sorry for the dutch words in it.
This is also my first question on stackoverflow, so if I missed any critical info you might need, please tell me.

Syntax Error, unexpected $end -- PHP error, what's wrong?

My entire error code is Parse error: syntax error, unexpected $end in /home/a3704125/public_html/home.php on line 356
Here is my entire PHP file.. Tell me what the problem may be? ._. Thanks!
<?php
define('INCLUDE_CHECK',true);
require 'connect.php';
require 'functions.php';
// Those two files can be included only if INCLUDE_CHECK is defined
session_name('GamesFXLogin');
// Starting the session
session_set_cookie_params(2*7*24*60*60);
// Making the cookie live for 2 weeks
session_start();
if($_SESSION['id'] && !isset($_COOKIE['GamesFXRemember']) && !$_SESSION['rememberMe'])
{
// If you are logged in, but you don't have the GamesFXRemember cookie (browser restart)
// and you have not checked the rememberMe checkbox:
$_SESSION = array();
session_destroy();
// Destroy the session
}
if(isset($_GET['logoff']))
{
$_SESSION = array();
session_destroy();
header("Location: home.php?logout=true");
exit;
}
if($_POST['submit']=='Login')
{
// Checking whether the Login form has been submitted
$err = array();
// Will hold our errors
if(!$_POST['username'] || !$_POST['password'])
$err[] = 'All the fields must be filled in!';
if(!count($err))
{
$_POST['username'] = mysql_real_escape_string($_POST['username']);
$_POST['password'] = mysql_real_escape_string($_POST['password']);
$_POST['rememberMe'] = (int)$_POST['rememberMe'];
// Escaping all input data
$row = mysql_fetch_assoc(mysql_query("SELECT id,usr FROM gamesfx_members WHERE usr='{$_POST['username']}' AND pass='".md5($_POST['password'])."'"));
if($row['usr'])
{
// If everything is OK login
$_SESSION['usr']=$row['usr'];
$_SESSION['id'] = $row['id'];
$_SESSION['rememberMe'] = $_POST['rememberMe'];
// Store some data in the session
setcookie('GamesFXRemember',$_POST['rememberMe']);
}
else $err[]='Wrong username and/or password!';
}
if($err)
$_SESSION['msg']['login-err'] = implode('<br />',$err);
// Save the error messages in the session
header("Location: index.php?page=home&error=true");
exit;
}
else if($_POST['submit']=='Register')
{
// If the Register form has been submitted
$err = array();
if(isset($_POST['submit']))
{
//whether the username is blank
if($_POST['username'] == '')
{
$err[] = 'User Name is required.';
}
if(strlen($_POST['username'])<4 || strlen($_POST['username'])>32)
{
$err[]='Your username must be between 3 and 32 characters!';
}
if(preg_match('/[^a-z0-9\-\_\.]+/i',$_POST['username']))
{
$err[]='Your username contains invalid characters!';
}
//whether the email is blank
if($_POST['email'] == '')
{
$err[]='E-mail is required.';
}
else
{
//whether the email format is correct
if(preg_match("/^([a-zA-Z0-9])+([a-zA-Z0-9._-])*#([a-zA-Z0-9_-])+([a-zA-Z0-9._-]+)+$/", $_POST['email']))
{
//if it has the correct format whether the email has already exist
$email= $_POST['email'];
$sql1 = "SELECT * FROM gamesfx_members WHERE email = '$email'";
$result1 = mysql_query($link,$sql1) or die(mysql_error());
if (mysql_num_rows($result1) > 0)
{
$err[]='This Email is already used.';
}
}
else
{
//this error will set if the email format is not correct
$err[]='Your email is not valid.';
}
}
//whether the password is blank
if($_POST['password'] == '')
{
$err[]='Password is required.';
}
if(!count($err))
{
// If there are no errors
// Make sure the email address is available:
if(!count($err))
{
$username = $_POST['username'];
$email = $_POST['email'];
$password = $_POST['password'];
$activation = md5(uniqid(rand()));
$encrypted=md5($password);
$sql2 = "INSERT INTO gamesfx_members (usr, email, pass, Activate) VALUES ('$username', '$email', '$encrypted', '$activation')";
$result2 = mysql_query($link,$sql2) or die(mysql_error());
if($result2)
{
$to = $email;
$subject = "Confirmation from GamesFX to $username";
$header = "GamesFX: Confirmation from GamesFX";
$message = "Please click the link below to verify and activate your account. rn";
$message .= "http://www.mysite.com/activate.php?key=$activation";
$sentmail = mail($to,$subject,$message,$header);
if($sentmail)
{
echo "Your Confirmation link Has Been Sent To Your Email Address.";
}
else
{
echo "Cannot send Confirmation link to your e-mail address";
}
}
exit();
}
}
$script = '';
if($_SESSION['msg'])
{
// The script below shows the sliding panel on page load
$script = '
<script type="text/javascript">
$(function(){
$("div#panel").show();
$("#toggle a").toggle();
});
</script>';
}
?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>A Cool Login System With PHP MySQL &amp jQuery | Tutorialzine demo</title>
<link rel="stylesheet" type="text/css" href="demo.css" media="screen" />
<link rel="stylesheet" type="text/css" href="css/slide.css" media="screen" />
<script type="text/javascript" src="http://ajax.googleapis.com/ajax/libs/jquery/1.3.2/jquery.min.js"></script>
<!-- PNG FIX for IE6 -->
<!-- http://24ways.org/2007/supersleight-transparent-png-in-ie6 -->
<!--[if lte IE 6]>
<script type="text/javascript" src="js/pngfix/supersleight-min.js"></script>
<![endif]-->
<script src="js/slide.js" type="text/javascript"></script>
<?php echo $script; ?>
</head>
<body>
<!-- Panel -->
<div id="toppanel">
<div id="panel">
<div class="content clearfix">
<div class="left">
<h1>The Sliding jQuery Panel</h1>
<h2>A register/login solution</h2>
<p class="grey">You are free to use this login and registration system in you sites!</p>
<h2>A Big Thanks</h2>
<p class="grey">This tutorial was built on top of Web-Kreation's amazing sliding panel.</p>
</div>
<?php
if(!$_SESSION['id']):
?>
<div class="left">
<!-- Login Form -->
<form class="clearfix" action="" method="post">
<h1>Member Login</h1>
<?php
if($_SESSION['msg']['login-err'])
{
echo '<div class="err">'.$_SESSION['msg']['login-err'].'</div>';
unset($_SESSION['msg']['login-err']);
}
?>
<label class="grey" for="username">Username:</label>
<input class="field" type="text" name="username" id="username" value="" size="23" />
<label class="grey" for="password">Password:</label>
<input class="field" type="password" name="password" id="password" size="23" />
<label><input name="rememberMe" id="rememberMe" type="checkbox" checked="checked" value="1" /> Remember me</label>
<div class="clear"></div>
<input type="submit" name="submit" value="Login" class="bt_login" />
</form>
</div>
<div class="left right">
<!-- Register Form -->
<form action="" method="post">
<h1>Not a member yet? Sign Up!</h1>
<?php
if($_SESSION['msg']['reg-err'])
{
echo '<div class="err">'.$_SESSION['msg']['reg-err'].'</div>';
unset($_SESSION['msg']['reg-err']);
}
if($_SESSION['msg']['reg-success'])
{
echo '<div class="success">'.$_SESSION['msg']['reg-success'].'</div>';
unset($_SESSION['msg']['reg-success']);
}
?>
<label class="grey" for="username">Username:</label>
<input class="field" type="text" name="username" id="username" value="" size="23" />
<label class="grey" for="email">Email:</label>
<input class="field" type="text" name="email" id="email" size="23" />
<label class="grey" for="password">Password:</label>
<input class="field" type="password" name="password" id="password" size="30" />
<label>A password will be e-mailed to you.</label>
<input type="submit" name="submit" value="Register" class="bt_register" />
</form>
</div>
<?php
else:
?>
<div class="left">
<h1>Members panel</h1>
<p>You can put member-only data here</p>
View your profile information and edit it
<p>- or -</p>
Log off
</div>
<div class="left right">
</div>
<?php
endif;
?>
</div>
</div> <!-- /login -->
<!-- The tab on top -->
<div class="tab">
<ul class="login">
<li class="left"> </li>
<li>Hello <?php echo $_SESSION['usr'] ? $_SESSION['usr'] : 'Guest';?>!</li>
<li class="sep">|</li>
<li id="toggle">
<a id="open" class="open" href="#"><?php echo $_SESSION['id']?'Open Panel':'Log In | Register';?></a>
<a id="close" style="display: none;" class="close" href="#">Close Panel</a>
</li>
<li class="right"> </li>
</ul>
</div> <!-- / top -->
</div> <!--panel -->
I am trying to use the slide panel that's a login panel.. Don't know if you ever heard of it. But anyhow, I am wondering how to fix this error. As-for I can't see what the problem may be.. I'm banging my head over it, thanks for the help!
EDIT: I added what's after the below this text..
<div class="pageContent">
<div id="main">
<div class="container">
<h1>A Cool Login System</h1>
<h2>Easy registration management with PHP & jQuery</h2>
</div>
<div class="container">
<p>This is a simple example site demonstrating the Cool Login System tutorial on <strong>Tutorialzine</strong>. You can start by clicking the <strong>Log In | Register</strong> button above. After registration, an email will be sent to you with your new password.</p>
<p>View a test page, only accessible by <strong>registered users</strong>.</p>
<p>The sliding jQuery panel, used in this example, was developed by Web-Kreation.</p>
<p>You are free to build upon this code and use it in your own sites.</p>
<div class="clear"></div>
</div>
<div class="container tutorial-info">
This is a tutorialzine demo. View the original tutorial, or download the source files. </div>
</div>
</div>
</body>
</html>
Closing brackets in here :
else if($_POST['submit']=='Register')
{
Put two closing brackets here:
$script = '';
}} #line 175
if($_SESSION['msg'])
Moral: always put opening and closing brackets together when going for any condition statement.

Include from displayed file or folder that contains

When using php do you set your path in reference to the files directory or from the page it is displayed from.
For example:
index.php is the home page of course
Directory structure.
index.php
includes > footer.php, header.php
product > product.php [blueproduct] > blueproduct.php
storescripts > connect_to_mysql.php, more.php
=================================================
Inside of footer I have a script that is not working on every page. Its a newsletter script to collect info. This is the code I'm using within my included footer.php:
</div>
<div class="footer">
<div class="wideNewsletter">
<div class="wrapNewsletter">
<div class="newsletterIntro"><b>NEWSLETTER SIGN UP</b></div>
<div class="newsletterForm">
<?php
$name = "";
$email = "";
$msg_to_user = "";
if ($_POST['name'] != "") {
include "../storescripts/connect_to_mysql.php";
// Be sure to filter this data to deter SQL injection, filter before querying database
$name = mysql_real_escape_string($_POST['name']);
$email = mysql_real_escape_string($_POST['email']);
$sql = mysql_query("SELECT * FROM newsletter WHERE email='$email'");
$numRows = mysql_num_rows($sql);
if (!$email) {
$msg_to_user = '<div class="warning"><ul><li>Please type an email address ' . $name . '.</li></ul></div><br /><br />';
} else if ($numRows > 0) {
$msg_to_user = '<div class="warning"><ul><li>' . $email . ' is already in the system.</li></ul></div><br /><br />';
} else {
$sql_insert = mysql_query("INSERT INTO newsletter (name, email, dateTime)
VALUES('$name','$email',now() )") or die (mysql_error());
$msg_to_user = '<div class="success"><ul><li>Thanks ' . $name . ', hope you find what you want!</li></ul></div><br /><br />';
$name = "";
$email = "";
}
$message = 'Name: ' . $_POST['name'] . ' Email: ' . $_POST['email'];
mail('newproducts#moniquetrinidadjewelry.com', 'New Newsletter Sign Up at Monique Trinidad Jewelry', $message);
}
?>
<form style="width:430px;" action="<?php echo $_SERVER['PHP_SELF']; ?>" method="post">
<fieldset style="text-align:left;padding:0px;border:0px;">
Name:
<input name="name" type="text" maxlength="36" value="<?php echo $name; ?>" />
Email:
<input name="email" type="text" maxlength="36" value="<?php echo $email; ?>" />
<input type="image" src="https://www.moniquetrinidadjewelry.com/images/new-images/green-bullet.png" border="0" name="mySubmitBtn" type="submit" value="Submit">
</fieldset>
</form></div>
<div style="position:absolute;top:120px;"><?php echo $msg_to_user; ?></div>
<div class="newsletterExplain">Receive product updates. Remember only one of each!</div>
</div>
</div><!--wide newletter end-->
<div class="wrapFooter">
<div class="tearOneFooter">
<div class="footerColumnList">
<div class="footerTitles">Connect With Us</div>
<div class="footerLists">
<ul>
<li>Connect With Monique!</li>
</ul>
</div>
</div>
<!--Seperate Connect With us Column from Information Column-->
<div class="footerColumnList">
<div class="footerTitles">Information</div>
<div class="footerLists">
<ul>
<li>About Us</li>
<li>Packaging</li>
<li>Terms & Conditions</li>
</ul>
</div>
</div>
<!--Seperate Information Column from Shipping and Returns Column-->
<div class="footerColumnList">
<div class="footerTitles">Shipping and Returns</div>
<div class="footerLists">
<ul>
<li><a href="https://www.moniquetrinidadjewelry.com/return-policy.php">Orders and Returns<a/></li>
<li>Secure Shopping</li>
<li></li>
</ul>
</div>
</div>
<!--Seperate Shipping and Returns Column from Services & Support Column-->
<div class="footerColumnList">
<div class="footerTitles">Hours Of Operation</div>
<div class="footerLists">
<ul>
<li>We are a 24/7 <br />Online Establishment!<br />(US Based)</li>
</ul>
</div>
</div>
<!--Seperate Connect With us Column from Information Column-->
</div>
<!--Beging SecondTearFooterArea-->
<div class="tearTwoFooter">
<!--<div class="signUpNewsLetter"><img src="https://www.moniquetrinidadjewelry.com/images/news_letter_temp_IMG.png" alt="newsletter" /></div>-->
<div class="paymentOptions"><img src="https://www.moniquetrinidadjewelry.com/images/payment_options_temp.png" alt="payment options" /></div>
<div class="twitter"><img src="https://www.moniquetrinidadjewelry.com/images/twitter_temp.png" alt="twitterLink" /></div>
</div>
</div>
</div>
</div>
This is working within the [blueproduct] directory from the product directory, but not the index.php.
I have another issue as well, but I believe better practice would be to open another question after I've done research on the issue correct? If not let me know and I'll edit this original message.
================================================================================
Edits and Additions Below
I have everything configured and I believe I can elminiate this as the issue. I'm really stumped on this one. The link to give you a better idea of what I mean is http://www.moniquetrinidadjewerly.com . If you go there and try the form it doesn't process, but that same form if you select 'necklace' within the navigation you can see works fine and runs correctly. Here is the updated footer.php file below to include changes for abs path.
</div>
<div class="footer">
<div class="wideNewsletter">
<div class="wrapNewsletter">
<div class="newsletterIntro"><b>NEWSLETTER SIGN UP</b></div>
<div class="newsletterForm">
<?php
$name = "";
$email = "";
$msg_to_user = "";
if ($_POST['name'] != "") {
include_once(DOC-ROOT."/storescripts/connect_to_mysql.php");
// Be sure to filter this data to deter SQL injection, filter before querying database
$name = mysql_real_escape_string($_POST['name']);
$email = mysql_real_escape_string($_POST['email']);
$sql = mysql_query("SELECT * FROM newsletter WHERE email='$email'");
$numRows = mysql_num_rows($sql);
if (!$email) {
$msg_to_user = '<div class="warning"><ul><li>Please type an email address ' . $name . '.</li></ul></div><br /><br />';
} else if ($numRows > 0) {
$msg_to_user = '<div class="warning"><ul><li>' . $email . ' is already in the system.</li></ul></div><br /><br />';
} else {
$sql_insert = mysql_query("INSERT INTO newsletter (name, email, dateTime)
VALUES('$name','$email',now() )") or die (mysql_error());
$msg_to_user = '<div class="success"><ul><li>Thanks ' . $name . ', hope you find what you want!</li></ul></div><br /><br />';
$name = "";
$email = "";
}
$message = 'Name: ' . $_POST['name'] . ' Email: ' . $_POST['email'];
mail('newproducts#moniquetrinidadjewelry.com', 'New Newsletter Sign Up at Monique Trinidad Jewelry', $message);
}
?>
<form style="width:430px;" action="<?php echo $_SERVER['PHP_SELF']; ?>" method="post">
<fieldset style="text-align:left;padding:0px;border:0px;">
Name:
<input name="name" type="text" maxlength="36" value="<?php echo $name; ?>" />
Email:
<input name="email" type="text" maxlength="36" value="<?php echo $email; ?>" />
<input type="image" src="https://www.moniquetrinidadjewelry.com/images/new-images/green-bullet.png" border="0" name="mySubmitBtn" type="submit" value="Submit">
</fieldset>
</form></div>
<div style="position:absolute;top:120px;"><?php echo $msg_to_user; ?></div>
<div class="newsletterExplain">Receive product updates. Remember only one of each!</div>
</div>
</div><!--wide newletter end-->
<div class="wrapFooter">
<div class="tearOneFooter">
<div class="footerColumnList">
<div class="footerTitles">Connect With Us</div>
<div class="footerLists">
<ul>
<li>Connect With Monique!</li>
</ul>
</div>
</div>
<!--Seperate Connect With us Column from Information Column-->
<div class="footerColumnList">
<div class="footerTitles">Information</div>
<div class="footerLists">
<ul>
<li>About Us</li>
<li>Packaging</li>
<li>Terms & Conditions</li>
</ul>
</div>
</div>
<!--Seperate Information Column from Shipping and Returns Column-->
<div class="footerColumnList">
<div class="footerTitles">Shipping and Returns</div>
<div class="footerLists">
<ul>
<li><a href="https://www.moniquetrinidadjewelry.com/return-policy.php">Orders and Returns<a/></li>
<li>Secure Shopping</li>
<li></li>
</ul>
</div>
</div>
<!--Seperate Shipping and Returns Column from Services & Support Column-->
<div class="footerColumnList">
<div class="footerTitles">Hours Of Operation</div>
<div class="footerLists">
<ul>
<li>We are a 24/7 <br />Online Establishment!<br />(US Based)</li>
</ul>
</div>
</div>
<!--Seperate Connect With us Column from Information Column-->
</div>
<!--Beging SecondTearFooterArea-->
<div class="tearTwoFooter">
<!--<div class="signUpNewsLetter"><img src="https://www.moniquetrinidadjewelry.com/images/news_letter_temp_IMG.png" alt="newsletter" /></div>-->
<div class="paymentOptions"><img src="https://www.moniquetrinidadjewelry.com/images/payment_options_temp.png" alt="payment options" /></div>
<div class="twitter"><img src="https://www.moniquetrinidadjewelry.com/images/twitter_temp.png" alt="twitterLink" /></div>
</div>
</div>
</div>
</div>
The config.inc.php file is located within the main directory and it reads :
<?php
define("Monique trinidad Jewelry","My Website");
define("DOC_ROOT","/home3/onlinfr7/public_html");
define("URL","https://www.moniquetrinidadjewelry.com");
?>
I'm not sure where the issue is occurring or what exactly is happening with the homepage(index.php) newsletter form in the footer. Why it works in one page, but not the other. It seems that path may not be the issue as I first thought. Any advice?
there's the current working directory, which you can get with getcwd(). THAT'S the path that everything will be relative to in any file operations you perform. By default it will be the directory that your main script is executed from.
Whether you go relative to that, or relative to something else, or just absolute on everything is up to you. There's no right/wrong answer - just whatever's easiest for YOU to maintain.
What I like to do that makes things easier when including other files is create a config file and include that in the main file or header like index.php
So this might be my config file called config.inc.php
<?php
define("SITENAME","My Website");
define("DOC_ROOT","/home/username/webroot");
define("URL","http://www.example.com");
?>
I include this config file in my index.php like
include("/home/username/webroot/config.inc.php");
Then I can use DOC_ROOT whenever I want to include another file somewhere and it will always have the full absolute path so that you know it's included.
e.g. include_once(DOC_ROOT."/storescripts/connect_to_mysql.php");

Categories