I have local host website. Connected two devices to a hotspot and accessing the website using the ip of the server. All the pages open properly but the session variable is not recognised as "Undefined Index" error throws up.
While when I access the same on the localhost I donot face this issue.
I have used session_start() in all the files.
Have the code snipped of Login.php
<?php
session_start();
?>
<html>
<head><title>LOGIN</title></head>
<body>
<form action= "Dashboard.php" method = "POST">
<br/>Name: <input type = "text" name = "name">
<br/>Password: <input type = "password" name = "password">
<br/><input type="submit" name = "submit" value = "submit"> or Register
</form>
</body>
</html>
<?php
if(isset($_POST['submit'])){
$name = $_POST['name'];
$password = $_POST['password'];
$connect = mysqli_connect("localhost","root", "","nets") or die("Couldn't connect to database");
$query = mysqli_query($connect,"SELECT * FROM users WHERE Name = '$name' and Password='$password'");
$count = mysqli_num_rows($query);
if($count == 1){
$_SESSION['username'] = $name;
echo "Login Successful";
}else{
echo "Invalid Login Credentials";
}
}
?>
After login Dashboard.php
<?php
session_start();
?>
<html>
<head><title>Dashboard</title></head>
<body>
<form action= "" method = "POST">
<?php echo "Welcome ".$_SESSION['username'].".<br>";
?>
<br/>Sex: <select name="sex"> <option value ="Male">Male</option> <option value = "Female">Female</option></select>
<br/>Age: <select name="years">
<?php
for($i=1; $i<=50; $i++)
{
echo "<option value=".$i.">".$i."</option>";
}
?>
<option name="years"> </option>
</select>
<br/>Citizen: <select name="citizen"><option value="Singaporean">Singaporean</option><option value="International">International</option></select>
Click to Download!
<br/><input type="submit" name = "submit2" value = "Save Changes"> orLogout
</form>
</body>
</html>
<?php
if(isset($_POST['submit2'])){
$name = $_SESSION['username'];
$connect = mysqli_connect("localhost","root", "","nets") or die("Couldn't connect to database");
$query1 = mysqli_query($connect,"SELECT Sex,Age,Citizen FROM users where Name = '$name'");
while ($row = mysqli_fetch_row($query1))
/* {
echo "Details Entered in Database:".".<br>";
echo "<br>";
echo "Name:".$_SESSION['username'].".<br>";
echo "Sex:".$row[0].".<br>";
echo "Age:".$row[1].".<br>";
echo "Citizen:".$row[2].".<br>";
}*/
$sex = $_POST['sex'];
$years = $_POST['years'];
$citizen = $_POST['citizen'];
$query2 = mysqli_query($connect, "UPDATE users SET Sex = '$sex', Age='$years', Citizen='$citizen' WHERE Name = '$name'");
echo "Saved changes";
}
?>
Please suggest
Your form has the action "Dashboard.php", so that script will handle the submission of your login form.
In other words, the logic inside the code block for the if statement if(isset($_POST['submit'])){ is probably not executed, so your session variable isn't set.
You can set the action to "Login.php" or leave it blank to let Login.php handle the login, then you can redirect to Dashboard.php if you desire and the session variable should be set.
Do mind that if you want to redirect using PHP, it must be done before the HTML. Otherwise it won't work.
Related
I am writing a form using php and mysql. The main goal is to make the form
(1) detect missing field.
(2) update user input after successful submit and
(3) most importantly to avoid re-submission on reload/refresh.
I am able to manage the first and the third one but doesn't have any idea on the second one.
Here's my code (able to achieve first and third)
form1.php
<!DOCTYPE html>
<html>
<head></head>
<body>
<?php
$name = "";
$class = "";
$school = "";
if(isset($_POST["submit"])){
$name = $_POST["name"];
$class = $_POST["class"];
$school = $_POST["school"];
$output = false;
if(empty($_POST["name"]) || empty($_POST["class"]) || empty($_POST["school"])){
echo 'field cannot be empty';
$output_form = true;
}
if(!empty($_POST["name"]) && !empty($_POST["class"]) && !empty($_POST["school"])){
$hostname = "localhost";
$admin = "root";
$password = "";
$database = "testdatabase";
$dbc = mysqli_connect($hostname, $admin, $password, $database) or die("database connection error");
$insert_query = "INSERT INTO `sorty` (`name`, `class`, `school`) VALUES ('$name', '$class', '$school')";
$insert_result = mysqli_query($dbc, $insert_query) or die("error");
if($insert_result == 1)
echo "data inserted";
else
echo "insert query failed";
mysqli_close($dbc);
header('Location: form2.php');
}
}
else
$output = true;
if($output){
?>
<form action="<?php echo $_SERVER['PHP_SELF'];?>" method="post">
Name: <input type="text" name="name" value="<?php echo $name?>"/><br/>
Class: <input type="text" name="class" value="<?php echo $class?>"/><br/>
School: <input type="text" name="school" value="<?php echo $school?>"/><br/>
<input type="submit" value="submit" name="submit"/>
</form>
<?php
}
?>
</body>
</html>
My second file form2.php(succesful page after form submission)
<body>
Name: /*user input here*/<br/>
Class: /*user input here*/<br/>
School: /*user input here*/<br/>
As I can't access the variable $name, $class, $school of form.php I am having problem updating the user input data. So is there anyway to access the variable across file or is it not possible to do in this way.
user_name you may check this out. and read the code. i hope you will get the answer. You may add session for showing the message that the specified operation is done. thank you :)
I've been working on a website login, and so far, I have the database and register page set up, but I'm trying to work on a Login page. I've been trying to retrieve data from the Database's Table. I was successfull at doing so on my register page to make sure there aren't multiple usernames of the same name, so I copied some of the code and pasted it onto this page. The problem: it returns blank. Please help... ._.
`
KHS SiteSpace
<div id="header">
<img src="./IMAGES/khslogo2.png" style="margin-left:4;float:left;" width="100" hieght="100">
<b>KHS<span id="name">SiteSpace</span></a>
<!--img src="./IMAGES/Menu.png" style="float:right;margin-right:6;" height="100" width="90"-->
</div>
<div id="content">
<p id="subTitle">Login</p>
<div style="float:left;height:30%;">
<form action="<?php echo $_SERVER['PHP_SELF'];?>" method="post" id="register"><br>
Username:<br>
<input type="text" name="name"><br><br>
Password:<br>
<input type="password" name="pass">
<br><br>
<input type="submit" value="Confirm">
</form>
</div>
<div style="float:right;width:50%;border-style:none none none solid; border-color:222222;border-width:4;height:30%;">
<p style="margin-left:20;font-size:20;">Output:</p>
<p style="margin-left:20;padding-bottom:15;">
<?php
error_reporting(0);
#ini_set('display_errors', 0);
session_start();
$conn = new mysqli("localhost", "shanedrgn", "getting321", "Users");
if (!$conn) {die("Failure to connect");}
$name = trim($_POST['name']);
$pass = trim($_POST['pass']);
if (empty($name) or empty($pass)) {echo "Empty Fields";} else {
$name = trim($_POST['name']);
$pass = trim($_POST['pass']);
echo "Check: The fields arent empty...";#OUTPUT
echo "Testing Variables...";#OUTPUT
//Error Trapping
$sql = "SELECT Username FROM Users where Username = '$name'";
$Data = mysqli_query($conn, $sql);
if($record=mysqli_fetch_array($Data)) {
$nameTrap = $record['Username'];
}
$sql = "SELECT Address FROM Users where Address = '$address'";
$Data = mysqli_query($conn, $sql);
if($record=mysqli_fetch_array($Data)) {
$ipTrap = $record['Address'];
}
if ($nameTrap == $name) {
echo "Check: Username Exists...";
if ($passTrap == $pass) {
echo "Password is correct!";
$_SESSION['User'] = $name;
$sql = "SELECT * FROM Users where Username = '$name'";
$Data = mysqli_query($conn, $sql);
$record=mysqli_fetch_array($Data);
session_start();
$_SESSION['id'] = $record['id'];
echo "<script>alert('You have successfully logged in!')</script>";
sleep(4);
header("Location: ./index.php"); /* Redirect browser */
exit();
} else { echo "Password Invalid";}
} else {echo "That username doesn't exist!";echo $name.";;";echo $nameTrap;}
}
?>
</p></div>
</div>
</body>
</html>`
EDIT: Added missing code
You are doing this:
echo "<script>alert('You have successfully logged in!')</script>";
sleep(4);
header("Location: ./index.php"); /* Redirect browser */
I understand, what you try, but it can't work. You can't set an Header after sending Body-Content - what you do using echo.
You should use JavaScript for your redirect, after the 4 second timeout. Use setTimeout and window.location.
On my website I've displayed a list of registered users on a members page using echo in PHP. I'd now like to link the names of these users to their profile pages. Is there a way to do this?
My current code:
<html>
<title>Find User Info</title>
<body>
<form method="POST">
<p>Type Username: </p><input type="text" name="username"placeholder="Enter Username...">
<br>
<input type="submit" name="submit" value="Search User">
</form>
<?php
session_start();
error_reporting(0);
$connection = mysql_connect("localhost", "root", "***"); // Establishing Connection with Server
$db = mysql_select_db("****", $connection); // Selecting Database from Server
$query = mysql_query("SELECT * FROM accs ORDER BY loginstatus"); //selecting all from table users where username is name that your is loged in
while($row = mysql_fetch_assoc($query))
{
echo $row['name'];
}
if(isset($_POST['submit'])){
$username = $_POST['username'];
$_SESSION['searchuser'] = $username; //setting session username to one from table, this is useful if you login, that restart your browser and than you go in url where is your profile.php... Anyway this is useful :D
header( 'Location: user.php' ) ;
}
?>
</body>
</html>
The template link is like: mywebsite.com/search/user.php
echo ''.$row['name'].'';
is this what you are looking for ?
All you'll have to do is echo out an <a> element with the appropriate parameters for the link.
In your loop where you echo out the name of the user. you can include the <a> element definitions and only have the name as the text of the link:
while($row = mysql_fetch_assoc($query)) {
$user_name = $row['name'];
$profile_link = "http://your-cool-site/users/" . $row['id'];
echo "<a href='" . $profile_link . "' >" . $user_name . "</a>";
}
This code assumes that the link to your user page is something like:
http://your-cool-site/users/USER_ID
I am trying to make an update in my database using two post variables, one from file clientpag.php and another from login.php. The variable $_POST['add'] which is an input from clientpag.php is not being saved. Can you help please? it is my first project using php!
LoginScript.php
<?php
session_start();
include_once'config/connect.php'; //database connection
if($_POST['submit']) {
$address = mysql_query("UPDATE venue SET location='".$_POST['add']."'
WHERE vid = (select vid from user_venue where id = (select user_id from iangadot_user
where username='".$_POST['username']."' ))");
while($row = mysql_fetch_assoc($address)){
$_SESSION['add'] = $row['location'];
}
header("Location: clientpag.php");
} else {
echo "<script language=javascript>alert('error')</script>";
}
?>
clientpag.php
<?php session_start(); ?>
<!DOCTYPE html>
<html>
<body>
<form action="LoginScript.php" method="POST">
<p>Address:</p> <input class = "field" name="add" placeholder= "<?php
if(isset($_SESSION['address'])) { echo $_SESSION['address']; } ?>" type="text" >
<input class = "button" type = "submit" Value = "Save"><br>
</form>
</body>
</html>
Change
<input class = "button" type = "submit" Value = "Save">
to
<input class = "button" type = "submit" Value = "Save" name="usubmit">
and then in PHP script change
if($_POST['submit'])
to
if($_POST['usubmit'] !='')
This should work unless there is an error in the query
we are working with php session data, but the data is not being stored in the database
login.php
<h2>Welcome</h2>
<form action = "Customer.php" method = "POST">
Customer Name:<input type = "text" name="customerName">
<input type = "submit" name = "submit">
</form>
Customer.php
<?php
session_start();
#include("Connection.php);
if (isset($_POST['submit'])) {
$name = $_POST['customerName'];
$_SESSION['user'] = $name;
}
if (isset($_SESSION['user']))
{
echo "Hello {$_SESSION['user']}, welcome to starbucks!";
}
else
{
echo "walang tao";
$sql="INSERT INTO `customer`.`people` ('ID', `NAME`) VALUES ('','$name')";
mysql_query($sql);
?>
Logout
and logout.php
<?php
session_start();
session_destroy();
?>
Click me to return to login
please tell me what's wrong
try displaying the mysql_error and see what the problem is
mysql_query($sql) or die(mysql_error());