Ir para conteúdo
Entre para seguir isso  
joaobarata

[Ajuda] Programar com php e sql

Publicações recomendadas

Como trabalho para uma cadeira de faculdade foi-me proposto o desenvolvimento de programação php e sql de modo a criar um site semelhante ao twitter (será apenas necessário que haja possibilidade em utilizadores fazerem login e que seja possivel que esses utilizadores escrevam mensagens que seja possivel de serem observadas). Para este trabalho só posso utilizar esses dois códigos e usar o xamp e o phpmyadmin. Consultando alguns tutoriais tentei resolver o trablho, no entanto nada funciona estando um autêntica trapalhada.

 

 

Este é o ficheiro index.php

<?php
session_start();
include_once('main_login.php');

$_SESSION['userid'] = 1;
?>
<!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" xml:lang="en" lang="en">
<head>
       <meta http-equiv="content-type" content="text/html; charset=utf-8" />
       <title>HUNTER</title>
</head>
<body>

<?php
if (isset($_SESSION['message'])){
       echo "<b>". $_SESSION['message']."</b>";
       unset($_SESSION['message']);
}
?>
<br />

<form method='post' action='add.php'>
<p>Your status:</p>
<textarea name='body' rows='5' cols='40' wrap=VIRTUAL></textarea>
<p><input type='submit' value='submit'/></p>
</form>


</body>
</html>

 

checklogin.php

<!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>Untitled Document</title>
</head>

<body>
<?php
$host="localhost"; // Host name 
$username="root"; // Mysql username 
$password=""; // Mysql password 
$db_name="novo"; // Database name 
$tbl_name="users"; // Table name

// Connect to server and select databse.
mysql_connect("$host", "$username", "$password")or die("<h3>Não consegue conectar</h3><br/>\n"); 
mysql_select_db("$db_name")or die("Não consegue ligar à base de dados");

// username and password sent from form 
$myusername=$_POST['myusername']; 
$mypassword=$_POST['mypassword'];

// encrypt password 
$encrypted_mypassword=md5($mypassword);

$sql="SELECT * FROM $tbl_name WHERE username='$myusername' and password='$encrypted_mypassword'";
$result=mysql_query($sql);

// To protect MySQL injection (more detail about MySQL injection)
$myusername = stripslashes($myusername);
$mypassword = stripslashes($mypassword);
$myusername = mysql_real_escape_string($myusername);
$mypassword = mysql_real_escape_string($mypassword);

$sql="SELECT * FROM $tbl_name WHERE username='$myusername' and password='$mypassword'";
$result=mysql_query($sql);

// Mysql_num_row is counting table row
$count=mysql_num_rows($result);
// If result matched $myusername and $mypassword, table row must be 1 row

if($count==1){
// Register $myusername, $mypassword and redirect to file "login_success.php"
session_register("myusername");
session_register("mypassword"); 
header("location:login_success.php");
}
else {
echo "Wrong Username or Password";
}
?>
</body>
</html>

 

add.php

<!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>Untitled Document</title>
</head>

<body><?php
session_start();
include_once("header.php");
include_once("functions.php");

$userid = $_SESSION['userid'];
$body = substr($_POST['body'],0,140);

add_post($userid,$body);
$_SESSION['message'] = "Your post has been added!";

header("Location:main_login.php");
?>
</body>
</html>

 

functions.php

<!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>Untitled Document</title>
</head>

<body>

<?
function add_post($userid,$body){
       $sql = "insert into posts (user_id, body, stamp)
                       values ($userid, '". mysql_real_escape_string($body). "',now())";

       $result = mysql_query($sql);
}
?>
<?
function show_posts($userid){
       $posts = array();

       $sql = "select body, stamp from posts
        where user_id = '$userid' order by stamp desc";
       $result = mysql_query($sql);

       while($data = mysql_fetch_object($result)){
               $posts[] = array(       'stamp' => $data->stamp,
                                                       'userid' => $userid,
                                                       'body' => $data->body
                                       );
       }
       return $posts;

}
?>
</body>
</html>

 

header.php

<!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>Untitled Document</title>
</head>
<body>
<?php

$SERVER = 'localhost';
$USER = 'root';
$PASS = '';
$DATABASE = 'novo';


if (!($mylink = mysql_connect( $SERVER, $USER, $PASS))){
       echo  "<h3>Não consegue ligar à base de dados</h3><br/>\n";
       exit;
}

mysql_select_db( $DATABASE );

?>
</body>
</html>

 

login_success.php

<!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>Untitled Document</title>
</head>

<body>
// Check if session is not registered , redirect back to main page. 
// Put this code in first line of web page. 
<? 
session_start();
if(!session_is_registered(myusername)){
header("location:main_login.php");
}
?>

Login Successful
</body>
</html>

 

logout.php

<!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>Untitled Document</title>
</head>

<body>
<? 
session_start();
session_destroy();
?>
</body>
</html>

 

main_login.php

<!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>Untitled Document</title>
</head>

<body bgcolor="white" text="blue">

<h1> HUNTER </h1>


<table width="300" border="0" align="center" cellpadding="0" cellspacing="1" bgcolor="#CCCCCC">
<tr>
<form name="form1" method="post" action="checklogin.php">
<td>
<table width="100%" border="0" cellpadding="3" cellspacing="1" bgcolor="#FFFFFF">
<tr>
<td colspan="3"><strong>Member Login </strong></td>
</tr>
<tr>
<td width="78">Username</td>
<td width="6">:</td>
<td width="294"><input name="myusername" type="text" id="myusername"></td>
</tr>
<tr>
<td>Password</td>
<td>:</td>
<td><input name="mypassword" type="text" id="mypassword"></td>
</tr>
<tr>
<td> </td>
<td> </td>
<td><input type="submit" name="Submit" value="Login"></td>
</tr>
</table>
</td>
</form>
</tr>
</table>




</body>
</html>

 

Basicamente o que eu pedia era que pudessem dizer o que está errado, em falta ou a mais.

Obrigado

Compartilhar este post


Link para o post

Crie uma conta ou entre para comentar

Você precisa de ser membro desta comunidade para poder comentar

Criar uma conta

Registe-se na nossa comunidade. É fácil!

Criar nova conta

Entrar

Já tem uma conta? Faça o login.

Autentique-se agora
Entre para seguir isso  

  • Todo o Mundial 2026 no CMPT
  • Outros membros neste tópico

    Nenhum utilizador registado está a visualizar esta página.

×
×
  • Criar Novo...