How to start a session? [closed] - php

Closed. This question needs debugging details. It is not currently accepting answers.
Edit the question to include desired behavior, a specific problem or error, and the shortest code necessary to reproduce the problem. This will help others answer the question.
Closed 8 years ago.
Improve this question
I want my earth.php file to find the users id when a user enter mysite.com/earth.php. I want this because I want the id to be placed in a sql code that will be sent to the database when the user enters mysite.com/earth.php. And I think I have to start a session when a user logs in to help the earth.php to find the user id? Can someone help me with that because I don't know how to do it.
This is my earth.php file:
<?php
error_reporting(E_ALL);
ini_set('display_errors', '1');
session_start();
$id = $_SESSION['id'];
session_write_close();
$verbindung = mysql_connect("localhost","root","******");
mysql_select_db("lan");
if(isset($_SESSION['id'])) {
mysql_query("UPDATE users SET ally='3' WHERE id='$id'");
}
?>
And this is the value I want to be changed for the user to 3 when they enters mysite.com/earth.php
This is my login.php file
Please help, I've been trying to solve this problem for a long time.

You could do something like this
<?php
define('DB_TYPE', 'mysql');
define('DB_HOST', '127.0.0.1');
define('DB_NAME', 'dbname');
define('DB_USER', 'root');
define('DB_PASS', 'password');
session_start();
try {
// create a new instance of a PDO connection
$db = new PDO(DB_TYPE.':host='.DB_HOST.';dbname='.DB_NAME, DB_USER, DB_PASS);
$db->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
}
catch(PDOException $e) {
// if the connection fails, display an error message
echo 'ERROR: ' . $e->getMessage();
}
if(isset($_POST['username']) && !empty($_POST['username'])) {
$username = $_POST['username'];
$sql = 'SELECT userid, username FROM users WHERE username = :username';
$stmt = $db->prepare($sql);
$stmt->bindValue('username', $username);
$stmt->execute();
$result = $stmt->fetchAll();
$_SESSION['id'] = $result[0]['userid'];
var_dump($_SESSION['id']); //this will show the the session
}
?>

As I see from your code, the $_SESSION['id'] is empty. Set a value for it. I see you edited your post. Then you just have to include your file after starting the session like
session_start();
include_once 'path/to/login/php/login.php;

Related

PHP: Session based login script won't work [closed]

Closed. This question needs debugging details. It is not currently accepting answers.
Edit the question to include desired behavior, a specific problem or error, and the shortest code necessary to reproduce the problem. This will help others answer the question.
Closed 6 years ago.
Improve this question
I got a simple login script that post the password and check the corrispondence with the database
session_start();
include 'core/config.php';
include 'core/db.php';
$checkPWD = $DB_CON -> query('SELECT * FROM users');
$checkPWD->execute();
$pwd = $checkPWD->fetchAll();
if($_POST['password']) {
$md5pwd = md5($_POST['password']);
if($md5pwd == $pwd[2]) {
$_SESSION['login'] = 'true';
$_GET['page'] = 'dashboard';
} else {
$_GET['error'] = 'true';
}
}
This exit and got $_GET['error'] set to true.
The password is stored in md5 in database.
Thanks a lot
You should do it in you query:
$md5pwd = md5($_POST['password']);
$checkPWD = $DB_CON->query("SELECT * FROM users WHERE password = '{$md5pwd}' LIMIT 1");
$pwd = $checkPWD->fetchAll();
if ($pwd) {
// Has find someone
} else {
// Hasn't find no one
}

PHP cannot show tables from MySqli Query

I am trying to figure out why I can connect to a database, but cannot access the data in it.
Here's my configuration:
//config.php
<?php
define("HOST", "MYSERVERNAMEISHERE");
define("DATABASE", "users");
?>
My user logs in, and their information is passed to be checked:
//login.php
<?php
if ($_POST) {
if ($_POST["user"] && $_POST["password"]) {
include_once "config.php";
define("USER", $_POST["user"]);
define("PASSWORD", $_POST["password"]);
$link = new mysqli(HOST, USER, PASSWORD, DATABASE);
if ($link) {
$link->close();
if ($_SESSION) {
session_destroy();
}
session_start();
$_SESSION["user"] = $_POST["user"];
$_SESSION["password"] = $_POST["password"];
}
}
}
if ($_SESSION) {
header('Location: profile.php');
}
else {
header('Location: login.html');
}
?>
When they pass, they get to see their profile page.
//profile.php
<?php
session_start();
if (!$_SESSION["user"] || !$_SESSION["password"]) {
session_destroy();
header("Location: login.html");
}
else {
include_once "config.php";
}
$link = new mysqli(HOST, USER, PASSWORD, DATABASE) or die("Unable to connect to database");
$result = $link->query("SHOW TABLES") or die("Unable to show tables");
...
ADDITIONAL PHP AND HTML CODE AFTER THIS POINT
The problem is that the process dies when I try to query the mysqli link. (I get Unable to show tables) Right now, the SHOW TABLES is just filler for debugging; I will actually have useful mysqli queries when I figure out the issue.
Please help me determine where my bug is. If you find a typo or a reference link for me, sorry for wasting your time. I've been researching and debugging for hours now.
Thanks very much in advance.
PS: If you have some good advice for changes I should make, I appreciate those too. It's my first time making a user login.
Your query in profile.php is failing because USER and PASSWORD are not defined. When the person logs in, they are defined in login.php. When redirected to profile.php, USER AND PASSWORD do not have values since they are not in config.php.
In profile.php, change
$link = new mysqli(HOST, USER, PASSWORD, DATABASE) or die("Unable to connect to database");
to
$link = new mysqli(HOST, $_SESSION["user"], $_SESSION["password"], DATABASE) or die("Unable to connect to database");

How do i connect to mysql server? and what do i use for the parameters?

Im trying to create a login for my website and i need to store emails, usernames, passwords, ect in a database i have created already using phpMyAdmin. I have gone through article after article and nothing seems to be working. i have my connect.php like this:
<?
$hostname = "localhost";
$username = "username";
$password = "password";
$databaseName = "_mySiteUserDataBase";
mysql_connect($hostname, $username, $password) or die("Cannot connect to server");
mysql_select_db($databaseName) or die("Cannot select database");
?>
And my main.php like this:
<?
include("connect.php");
$tableName = "myUsers";
$sql = "SELECT * FROM $tableName";
$result = mysql_query($sql);
?>
And i have created a simple form in my html like this:
<html>
<head></head>
<body>
<form>
<input type = "submit" action = "main.php" method = "post" value = "Login">
</form>
</body>
</html>
After submitting the form it says cannot connect to server. I am new to php and mysql and i dont understand what each parameter in the mysql_connect is, and i dont know what they do therefore im not sure what im supposed to enter in but everyone i keep reading about seems to be inputing random values? I could use a brief explanation on that, because i am stuck at connecting and cant even get past this point sadly enough. Also i have been reading that mysql_connect is deprecated and isnt valid anymore but i dont understand what im supposed to use as an alternative. I know its mysqli but thats it and im unclear of the syntax.
mysqli:
<?php
mysqli_report(MYSQLI_REPORT_ERROR | MYSQLI_REPORT_STRICT);
echo "start<br/>";
try {
$mysqli= new mysqli('localhost', 'myusername', 'mypassword', 'dbname');
if ($mysqli->connect_error) {
die('Connect Error (' . $mysqli->connect_errno . ') '
. $mysqli->connect_error);
}
echo "I am connected and feel happy.<br/>";
$mysqli->close();
} catch (mysqli_sql_exception $e) {
throw $e;
}
?>
If you need to know how to create users, what the heck the hostname is, how to grant access (often useful after the connect :>), just ask.
Try this code in 'connect.php'
<?php
error_reporting(0);
$con=mysql_connect('localhost','root','');// here 'root' is your username and "" is password
if(!$con)
{
echo 'not connect';die;
}
mysql_select_db('dbname',$con);// here 'dbname' is your database name
?>
And also try following code to include sql connection in your other php file(main.php)
<?php
include 'connect.php';
$sql = "SELECT * FROM myUsers";
$result=mysql_query($sql);
?>
Let me convert it to mysqli for you and maybe that will fix the problem. Also, make sure the username, password, and database name are correct.
Try this code. At very least, it will provide a better error message for debugging.
<?
$hostname = "localhost";
$username = "username";
$password = "password";
$databaseName = "_mySiteUserDataBase";
$con = mysqli_connect($hostname, $username, $password, $databaseName) or die(mysqli_error($con));
?>
Main.php
<?
include("connect.php");
$tableName = "myUsers";
$sql = "SELECT * FROM $tableName";
$result = mysqli_query($con,$sql);
?>

global variable not working in php class [closed]

Closed. This question needs debugging details. It is not currently accepting answers.
Edit the question to include desired behavior, a specific problem or error, and the shortest code necessary to reproduce the problem. This will help others answer the question.
Closed 7 years ago.
Improve this question
class Database
{
public $connect = "";
public function connect()
{
$connect = new mysqli("localhost", "root", "1521", "phone");
ini_set('default_charset', "UTF-8");
mysqli_set_charset($connect, "utf8");
header('Content-type: text/html; charset=UTF-8');
if ($connect->connect_error) {
die('Not connect' . mysqli_connect_error());
}
}
public function insert()
{
$sql = "SELECT id FROM personal ORDER BY id DESC LIMIT 1";
//global $connect;
$result = $connect->query($sql);//// this here !!!!!!!!
$row = $result->fetch_assoc();
$tempint = (int)$row['id'];
$tempint += 1;
}
}
why $connect in insert function not working ?
I added global befor $connect but still not working.
this very simple class for MySQL.
You syntax is incorrect.
Change this,
$connect = new mysqli("localhost", "root", "1521", "phone");
To,
$this->connect = new mysqli("localhost", "root", "1521", "phone");
Also,
$result = $connect->query($sql);
To,
$result = $this->connect->query($sql);
With the above changes it should fix the issue.
$connect has been created in the scope of the class so when you refer to it you need to use $this.

how to connect to a mysql database [closed]

Closed. This question needs debugging details. It is not currently accepting answers.
Edit the question to include desired behavior, a specific problem or error, and the shortest code necessary to reproduce the problem. This will help others answer the question.
Closed 7 years ago.
Improve this question
How do I connect to a MySQL database in PHP?
Database name: NewDB1
Username : Mark
Password : secret
I have googled a few solutions, but none of them seem to work.
<?php
$servername = "localhost";
$username = "Mark";
$password = "secret";
// Create connection
$conn = new mysqli($servername, $username, $password);
// Check connection
if ($conn->connect_error) {
die("Connection failed: " . $conn->connect_error);
}
echo "Connected successfully";
?>
With PDO -
<?php
// turn on error reporting and display
error_reporting(E_ALL);
ini_set('display_errors', 1);
// define the user
define('USER', 'Mark');
define('PASS', 'secret');
// establish database connection
try {
$dbh = new PDO('mysql:host=localhost;dbname=NewDB1', USER, PASS);
$dbh->setAttribute(PDO::ATTR_EMULATE_PREPARES, false);
$dbh->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION); // make sure to turn on error reporting
}
catch(PDOException $e) {
echo $e->getMessage(); // show any error messages.
$errorCode = $e->getCode();
}
?>
This comes from a more detailed tutorial on how to understand and simplify PDO.

Categories