Jump to content

ibnclaudius

Members
  • Posts

    40
  • Joined

  • Last visited

    Never

Posts posted by ibnclaudius

  1. Is there a better way to do this?

     

    $formatted_bloomp = '<div data-id="' . $bloomp['id'] . '" class="bloomp invisible">';
            if ($bloomp['image_id']) {
                $formatted_bloomp .= '<div class="media image">';
                $formatted_bloomp .= '<img src="' . base_url('upload/image/' . $bloomp['image_id']) . '">';
                $formatted_bloomp .= '</div>';
            } elseif ($bloomp['video_id'] && $bloomp['video_provider']) {
                $formatted_bloomp .= '<div class="media video">';
                if ($bloomp['video_provider'] == 'youtube') {
                    $formatted_bloomp .= '<iframe width="310" height="233" src="http://www.youtube.com/embed/' .  $bloomp['video_id'] . '?rel=0&wmode=transparent&iv_load_policy=3&color=white" frameborder="0" allowfullscreen></iframe>';
                } if ($bloomp['video_provider'] == 'vimeo') {
                    $formatted_bloomp .= '<iframe src="http://player.vimeo.com/video/' . $bloomp['video_id'] . '?color=#ffffff" width="310" height="233" frameborder="0" webkitAllowFullScreen mozallowfullscreen allowFullScreen></iframe>';
                }
                $formatted_bloomp .= '</div>';
            }
            $formatted_bloomp .= '<span class="text">' . $bloomp['text'] . '</span>';
            $formatted_bloomp .= '<div class="info">';
            $formatted_bloomp .= '<span>';
            $formatted_bloomp .= '<a class="author" href="#">';
            $formatted_bloomp .= '<img src="' .  base_url('upload/picture/' . $bloomp['author_picture']) . '">';
            $formatted_bloomp .= '<span class="name">' . $bloomp['author_name'] . '</span>';
            $formatted_bloomp .= '</a>';
            $formatted_bloomp .= '</span>';
            $formatted_bloomp .= '<span>' . $bloomp['likes_number'] . ' curtidas</span>';
            $formatted_bloomp .= '<span>' . $bloomp['comments_number'] . ' coment?rios</span>';
            $formatted_bloomp .= '</div>';
            if ($bloomp['comments_number'] > 0) {
                $formatted_bloomp .= '<div class="comments">';
                foreach ($this->_CI->quickstart_model->read_comments(3, $bloomp['id']) as $comment) {
                    $formatted_bloomp .= '<div data-id="' . $comment['id'] . '" class="comment clearfix">';
                    $formatted_bloomp .= '<a href="#"><img class="float-left" src="' . base_url('upload/picture/'. $comment['author_picture']) . '"></a>';
                    $formatted_bloomp .= '<span class="float-right">';
                    $formatted_bloomp .= '<a href="#">' . $comment['author_name'] . '</a> ' . $comment['text'];
                    $formatted_bloomp .= '</span>';
                    $formatted_bloomp .= '</div>';
                }
                if ($bloomp['comments_number'] > 3) {
                    $formatted_bloomp .= '<div class="comment all">';
                    $formatted_bloomp .= '<a href="#">Todos os ' . $bloomp['comments_number'] . ' coment?rios...</a>';
                    $formatted_bloomp .= '</div>';
                }
                $formatted_bloomp .= '</div>';
            }
            $formatted_bloomp .= '</div>';
            return $formatted_bloomp;
    

  2. I want to vertical align the label with the input no matter the label size.

     

    Here's my code and what it output:

     

    <div class="form-item">
    	<div class="form-label strong left">Nome:</div>
    	<div class="form-input-wrapper left">
    		<input class="form-input" type="text">
    	</div>
    </div>
    <div class="form-item">
    	<div class="form-label strong left">Email:</div>
    	<div class="form-input-wrapper left">
    		<input class="form-input" type="text">
    	</div>
    </div>
    <div class="form-item">
    	<div class="form-label strong left">Email novamenteEEEEEEEEEEEEEE:</div>
    	<div class="form-input-wrapper left">
    		<input class="form-input" type="text">
    	</div>
    </div>

     

    .form-item
    {
    border-bottom:1px solid #eee;
    overflow:hidden;
    padding:15px 20px
    }
    
    .form-label
    {
    color:#666;
    position:absolute;
    text-align:right;
    width:100px;
    }
    
    .form-input
    {
    display:block;
    width:232px;
    }

     

    labelinput.png

  3. Thanks, all!

     

    I made some changes, almost everything is correct now, the only problem is when there's only one user that liked, his name or you do not output.

     

    if ($count_school_update_likes > 0)
    							{
    								$likers = array();
    
    								if ($count_school_update_likes > 1)
    								{
    									$i = 0;
    
    									if ($count_school_update_liked == 1)
    									{
    										$likers[] = 'Você';
    										$i = 1;
    									}
    
    									if ($get_school_update_likes)
    									{
    										foreach ($get_school_update_likes->result_array() as $liker)
    										{
    											$likers[] = '<a id="' . $liker['user_id'] . '" href="' . $liker['user_id'] . '">' . $liker['user_name'] . '</a>';
    
    											$i++;
    
    											if ($i == 3)
    											{
    												$count_school_update_likes = $count_school_update_likes - 3;
    
    												if ($count_school_update_likes > 0)
    												{
    													$likers[] = 'mais <a id="stream-updates-item-likes-view" href="#">' . $count_school_update_likes . '</a> ';
    												}
    
    												break;
    											}
    										}
    									}
    								}
    
    								$last_liker = count($likers) > 1 ? ' e ' . array_pop($likers) . ' curtiram' : ' curtiu';
    
    								$likers = implode(', ', $likers) . $last_liker . ' isto';
    
    								echo $likers;
    							}

  4. jcbones, I liked that you keep the format :)

     

    But there's some little bugs. when only the user likers, returned this:

     

    and You like this.

     

    Fixed with:

     

    $last_likers = count($likers) > 1 ? ' and ' . array_pop($likers) : '';

     

    And when more than one user liked, returned this:

     

    You and Derp, like this.
  5. A preg_replace like this would also work:

     

    if ($count_school_update_likes == '1')
    {
    //remove last comma from $likers
    }
    else if ($count_school_update_likes > '1')
    {
    //remove last comma from $likers and replace the second last comma for "and"
    }
    

     

    Or something that work on both cases...

  6. When i ran:

     

    $likers = $get_school_update_likes->result_array();
    $lastLiker = count($likers) > 1 ? ' and ' . array_pop($likers) : '';
    $likers = implode(', ', $likers); 
    
    echo $likers . $lastLiker . ' like this.';
    

     

    i got this:

     

    A PHP Error was encountered
    
    Severity: Notice
    
    Message: Array to string conversion
    
    Filename: libraries/action.php
    
    Line Number: 230
    
    Array and Array like this

     

    And:

     

    print_r($likers)

     

    outputs something like this:

     

    Array ( [0] => Array ( [user_id] => 3 [user_name] => Derp ) [1] => Array ( [user_id] => 4 [user_name] => Derpina ) ) 

  7. Here's my code:

     

    if ($count_school_update_likes > '0')
    {
    $likers = '';
    
    if ($count_school_update_liked == "1")
    {
    	$likers .= 'You, ';
    }
    
    if ($count_school_update_likes > '1')
    {
    	$i = 0;
    
    	foreach ($get_school_update_likes->result_array() as $liker)
    	{
    		$likers .= '<a id="' . $liker['user_id'] . '" href="' . $liker['user_id'] . '">' . $liker['user_name'] . '</a>, ';
    
    		$i++;
    
    		if ($i == 2)
    		{
    			$count_school_update_likes = $count_school_update_likes - 3;
    
    			if ($count_school_update_likes > "0")
    			{
    				$likers .= ' and <a id="stream-updates-item-likes-view" href="#">' . $count_school_update_likes . ' others</a> ';
    			}
    
    			break;
    		}
    	}
    }
    
    $likers .= ' like this.';
    
    echo preg_replace("/,([^,]*)$/", " $1", $likers); //is not working correctly
    }

     

    Here is all the possibles outputs I would like to have:

     

    You, like this => You like this

    You, Derp, like this => You and Derp like this

    You, Derp, Derpina, like this => You, Derp and Derpina like this

    You, Derp, Derpina, Derpson1, Derpson2, Derpson3, Derpson4, like this => You, Derp, Derpina and other 4 like this

    Derp, like this => Derp like this

    Derp, Derpina, like this => Derp and Derpina like this

    Derp, Derpina, Derpson, like this => Derp, Derpina and Derpson like this

    Derp, Derpina, Derpson1, Derpson2, Derpson3, Derpson4, like this => Derp, Derpina, Derpson1 and other 3 like this

  8. How can I replace the comma from a string like this:

     

    You, like this => You like this

    You, Derp, like this => You and Derp like this

    You, Derp, Derpina, like this => You, Derp and Derpina like this

    You, Derp, Derpina, Derpson, like this => You, Derp, Derpina and Derpison like this

  9. Not sure if everything is working right because I am in the school computer and here I can not install apache / php / etc.

     

    I'm not sure about how to use cookies.

     

    Here is my code, any errors or suggestions just talk. Probably does not work, because as I said, not yet tested.

     

    login.php

    <?php
    
    session_start();
    
    include 'class.php';
    
    if (isset($_POST['username']) && isset($_POST['password'])) {
    $user  = new User($_POST['username']);
    if ($user->exists()) {
    	$login = $user->login($_POST['password']);
    	if ($login) {
    		$_SESSION['user_id'] = $login;
    		session_write_close();
    	} else {
    		echo "Login failed.";
    	}
    } else {
    	header("Location: register.php");
    }
    }
    
    ?>
    
    <!DOCTYPE html>
    <html>
    <head>
    	<title>Login Form</title>
    </head>
    <body>
    	<form action="" method="post">
    		<label for="username">Username: </label>
    		<input type="text" name="username" /><br />
    		<label for="password">Password: </label>
    		<input type="password" name="password" /><br />
    		<input type="submit" value="Submit" />
    	</form>
    </body>
    </html>

     

    class.php

    <?php
    
    class User {
    protected $id;
    protected $username;
    protected $email;
    protected $sql;
    
    private $exists = FALSE;
    
    public function __construct($username) {
    	if (empty($username)) {
    		throw new Exception('Username cannot be blank.');
    	}
    
    	$this->username = $username;
    	$this->sql      = new PDO(DSN, DBUSER, DBPASS);
    	$this->exists   = $this->validate();
    }
    
    private function createLoginToken($id) {
    	$token   = $id . md5(microtime());
    	$expires = new DateTime();
    
    	$expires->add(new DateInterval('P30D'));
    
    	$query = "INSERT INTO sessions (userID, token, expires)
    		  VALUES (:id, :token, :expires)";
    	$stmt  = $this->sql->prepare($query);
    
    	$stmt->execute(array(':id'      => $id,
    			     ':token'   => $token,
    			     ':expires' => $expires->format('Y-m-d H:i:s')));
    
    	setcookie('token', $token, $expires->getTimestamp(), '/');
    }
    
    private function hashPassword($password, $salt) {
    	$string = PASSWORD_SALT . $password . md5($salt);
    	$hashed = crypt($string, '$2a$12$' . substr(md5($salt), 0, 22));
    
    	return $hashed;
    }
    
    private function validate() {
    	$query = "SELECT COUNT(id)
    		  FROM users
    		  WHERE username = :username";
    	$stmt  = $this->sql->prepare($query);
    
    	$stmt->execute(array(':username' => $this->username));
    	$count = $stmt->fetchColumn();
    
    	return ($count > 0) ? TRUE : FALSE;
    }
    
    public function exists() {
    	return $this->exists;
    }
    
    public function login($password, $remember = FALSE) {
    	$query = "SELECT id, password, UNIX_TIMESTAMP(created) AS salt
    		  FROM users
    		  WHERE username = :username";
    	$stmt  = $this->sql->prepare($query);
    
    	$stmt->execute(array(':username' => $this->username));
    
    	$row = $stmt->fetch(PDO::FETCH_OBJ);
    
    	$hashed = $this->hashPassword($password, $row->salt);
    
    	if ($row->password == $hashed) {
    		if ($remember) {
    			$this->createLoginToken($row->id);
    		}
    		return $row->id;
    	}
    
    	return FALSE;
    }
    
    public function random() {
    	$random = mt_random(1000,9999);
    
    	return $random;
    }
    
    public function registerUser($email) {
    	if (!filter_var($email, FILTER_VALIDATE_EMAIL)) {
    		throw new Exception('Email does not appear to be valid.');
    	}
    	$this->email = $email;
    
    	$date   = new DateTime();
    	$hashed = $this->hashPassword($pass->random(), $date->getTimestamp());
    
    	$query = "INSERT INTO users (username, password, email, created)
    		  VALUES (:username, :password, :email, :created)";
    	$stmt  = $this->sql->prepare($query);
    
    	$success = $stmt->execute(array(':username' => $this->username,
    					':password' => $hashed,
    					':email'    => $email,
    					':created'  => $date->format('Y-m-d H:i:s')));
    
    	return ($success === TRUE) ? $this->sql->lastInsertId() : FALSE;
    }
    
    public function verifyCookie($token) {
    	$query = "SELECT userID
    		  FROM sessions
    		  WHERE token = :token
    		  AND expires > NOW()";
    	$stmt  = $this->sql->prepare($query);
    
    	$stmt->execute(array(':token' => $token));
    
    	return $stmt->fetchColumn();
    }
    }
    
    ?>

     

    db.sql

    CREATE TABLE IF NOT EXISTS `users` (
    `id` int(11) DEFAULT NULL AUTO_INCREMENT,
    `username` varchar(30) DEFAULT NULL,
    `password` varchar(60) DEFAULT NULL,
    `email` varchar(100) DEFAULT NULL UNIQUE,
    `created` datetime DEFAULT NULL,
    PRIMARY KEY (`id`)
    ) ENGINE=InnoDB;
    
    CREATE TABLE IF NOT EXISTS `sessions` (
    `id` int(11) DEFAULT NULL AUTO_INCREMENT,
    `userID` int(10) DEFAULT NULL,
    `token` varchar(50) DEFAULT NULL,
    `expires` datetime DEFAULT NULL,
    KEY `userID` (`id`,`userID`,`token`,`expires`),
    KEY `token` (`id`,`userID`,`token`,`expires`),
    PRIMARY KEY (`id`)
    ) ENGINE=InnoDB;

  10. A few more changes, check it.

     

    <?php
    
    
    
    class network {
    
    public $userID;
    
    public $schoolID;
    
    public $userEnrollment;
    
    public $userName;
    
    public $dbUserTable;
    
    public $dbSchoolTable;
    
    
    
    protected $sql;
    
    
    
    public function __construct($dbHost, $dbUser, $dbPass, $dbName)
    
    {
    
    	$dsn = "mysql:host={$dbHost};dbname={$dbName}";
    
    	try
    
    	{
    
    		$this->sql = new PDO($dsn, $dbUser, $dbPass);
    
    	}
    
    	catch (PDOException $e)
    
    	{
    
    		throw new Exceptopn($e->getMessage());
    
    	}
    
    
    
    	$this->dbUserTable   = $dbUserTable;
    
    	$this->dbSchoolTable = $dbSchoolTable;
    
    }
    
    
    
    public function registerUser($userEnrollment, $userName, $userPass)
    
    {
    
    	$this->userName = $userName;
    
    	$hashedPass = $this->hashPassword($userPass);
    
    
    
    	$query = "INSERT INTO {$this->dbUserTable} VALUES (NULL, :enrollment, :username, :password)";
    
    	$stmt  = $this->sql->prepare($query);
    
    
    
    	$result = $stmt->execute(array(':enrollment' => $userEnrollment,
    
                                           ':username'   => $userName,
    
                                           ':password'   => $hashedPass));
    
    
    
    	return ($result === TRUE) ? $this->sql->lastInsertId() : FALSE;
    
    }
    
    
    
    public function registerSchool($schoolName)
    
    {
    
    	$this->schoolName = $schoolName;
    
    
    
    	$query = "INSERT INTO {$this->dbSchoolTable} VALUES (NULL, :schoolName)";
    
    	$stmt  = $this->sql->prepare($query);
    
    
    
    	$result = $stmt->execute(array(':schoolName' => $schoolName));
    
    
    
    	return ($result === TRUE) ? $this->sql->lastInsertId() : FALSE;
    
    }
    
    
    
    public function userLogin()
    
    {
    
    	$dbLink = mysql_connect($this->dbHost, $this->dbUser, $this->dbPass);
    
    
    
    	if (!$dbLink) die("Could not connect to database: " . mysql_error());
    
    
    
    	mysql_select_db($this->dbName);
    
    
    
    	$query = "SELECT * FROM $this->dbUserTable WHERE userEnrollment = \"$this->userEnrollment\" AND userPass = \"$this->userPass\" LIMIT 1";
    
    
    
    	$result = mysql_query($query);
    
    
    
    	if (!$result)
    
    	{
    
    		echo "Fail.";
    
    	}
    
    	else
    
    	{
    
    		$row = mysql_fetch_array($result);
    
    
    
    		session_regenerate_id();
    
    			$_SESSION['userEnrollment'] = $this->userEnrollment;
    
    		session_write_close();
    
    	}
    
    
    
    	mysql_close($dbLink);
    
    }
    
    
    
    public function changePass($newPass)
    
    {
    
    	$query = "SELECT COUNT(*)
    
    		FROM {$this->dbUserTable}
    
    		WHERE userName = :username
    
    		LIMIT 1";
    
    
    	$stmt  = $this->sql->prepare($query);
    
    
    
    	$result = $stmt->execute(array(':username' => $this->userName));
    
    
    
    	if (!$result)
    
    	{
    
    		throw new Exception('User does not exist.');
    
    	}
    
    	else
    
    	{
    
    		$hashedPass = $this->hashPassword($newPass);
    
    
    
    		$query = "UPDATE {$this->dbUserTable}
    
    			SET userPass = :password
    
    			WHERE userName = :username";
    
    
    		$stmt  = $this->sql->prepare($query);
    
    
    
    		$result = $stmt->execute(array(':password' => $hashedPass,
    
    						':username' => $this->userName));
    
    
    
    		return ($result === TRUE) ? TRUE : FALSE;
    
    	}
    
    }
    
    
    
    private function hashPassword($password)
    
    {
    
    	$salt   = "This shouldn't really be hard-coded into the function";
    
    	$hashed = crypt($password, '$2a$12$' . substr(md5($salt), 0, 22));
    
    
    
    	return $hashed;
    
    }
    
    }
    
    
    
    ?>

     

    What should I change to the code have a better "functioning"?

     

    I'm not sure about how to call this class and function. How should I make a login form?

  11. Oh, I didnt restart it. Now I see the erros.

     

    I fixed the ')' problem and change this:

    var $userID,
    
    		$userEnrollment,
    
    		$userPass,
    
    		$dbHost,
    
    		$dbUser,
    
    		$dbName,
    
    		$dbPass,
    
    		$dbUserTable;

    to:

    public $userID;
    
    	public $userEnrollment;
    
    	public $userPass;
    
    	public $dbHost;
    
    	public $dbUser;
    
    	public $dbName;
    
    	public $dbPass;
    
    	public $dbUserTable;
    

     

    Now seems to be correct, I got this error because I dind set dbInfo yet:

    Warning: mysql_real_escape_string(): Access denied for user 'www-data'@'localhost' (using password: NO) in /var/www/portal_escolar/system/ajax_login.php on line 9 Warning: mysql_real_escape_string(): A link to the server could not be established in /var/www/portal_escolar/system/ajax_login.php on line 9 Warning: mysql_connect(): Access denied for user 'www-data'@'localhost' (using password: NO) in /var/www/portal_escolar/system/class/network.php on line 68 Could not connect to database: Access denied for user 'www-data'@'localhost' (using password: NO)

     

    I'll set it and give feedback soon.

  12. I'm newbie in PHP and english is not my native language, this makes it a little harder

     

    Could you give me an example of what you said?

     

    Firstly, it is tightly coupled with the mysql extension. Instead, you should be passing a database object (implementing an interface that this class recognises) into this object.

     

    Secondly, there are several places in the class that output error messages within this class. Classes should not output anything (unless that is what they are designed to do) but instead throw exceptions or have methods simply return false.

     

    Other than that, it's a pretty good start. I would however recommend using the more common php5 syntax instead of the php4 syntax.

     

    I made some changes, check it!

    <?php
    
    
    
    class network {
    
    
    
    	public $userID;
    
    	public $schoolID;
    
    	public $userEnrollment;
    
    	public $userName;
    
    	public $userPass;
    
    	public $dbHost;
    
    	public $dbUser;
    
    	public $dbName;
    
    	public $dbPass;
    
    	public $dbUserTable;
    
    	public $dbSchoolTable;
    
    
    
    	function dbInfo() {
    
    		$this->dbHost = 'localhost';
    
    		$this->dbUser = '';
    
    		$this->dbPass = '';
    
    		$this->dbName = '';
    
    		$this->dbUserTable = '';
    
    		$this->dbSchoolTable = '';
    
    	}
    
    
    
    	function registerUser($userEnrollment, $userName, $userPass) {
    
    		$dbLink = mysql_connect($this->dbHost, $this->dbUser, $this->dbPass);
    
    		if(!$dbLink) die("Could not connect to database: " . mysql_error());
    
    
    
    		mysql_select_db($this->dbName);
    
    
    
    		$query = "INSERT INTO $this->dbUserTable VALUES (NULL, \"$userEnrollment\", \"$userName\", \"$userPass\")";
    
    		$result = mysql_query($query);
    
    
    
    		if(!$result) {
    
    			echo "Fail.";
    
    		} else {
    
    			$this->userID = mysql_insert_id();
    
    		}
    
    
    
    		mysql_close($dbLink);
    
    
    
    		$this->userName = $userName;
    
    		$this->userPass = $userPass;
    
    	}
    
    
    
    	function registerSchool($schoolName) {
    
    		$dbLink = mysql_connect($this->dbHost, $this->dbUser, $this->dbPass);
    
    		if(!$dbLink) die("Could not connect to database: " . mysql_error());
    
    
    
    		mysql_select_db($this->dbName);
    
    
    
    		$query = "INSERT INTO $this->dbSchoolTable VALUES (NULL, \"$schoolName\")";
    
    		$result = mysql_query($query);
    
    
    
    		if(!$result) {
    
    			echo "Fail.";
    
    		} else {
    
    			$this->schoolID = mysql_insert_id();
    
    		}
    
    
    
    		mysql_close($dbLink);
    
    
    
    		$this->schoolName = $schoolName;
    
    	}
    
    
    
    	function userLogin() {
    
    		$dbLink = mysql_connect($this->dbHost, $this->dbUser, $this->dbPass);
    
    		if(!$dbLink) die("Could not connect to database: " . mysql_error());
    
    
    
    		mysql_select_db($this->dbName);
    
    
    
    		$query = "SELECT * FROM $this->dbUserTable WHERE userEnrollment = \"$this->userEnrollment\" AND userPass = \"$this->userPass\" LIMIT 1";
    
    		$result = mysql_query($query);
    
    
    
    		if(!$result) {
    
    			echo "Fail.";
    
    		} else {
    
    			$row = mysql_fetch_array($result);
    
    			session_regenerate_id();
    
    				$_SESSION['userEnrollment'] = $this->userEnrollment;
    
    			session_write_close();
    
    		}
    
    
    
    		mysql_close($dbLink);
    
    	}
    
    
    
    	function changePass($newPass) {
    
    		$dbLink = mysql_connect($this->dbHost, $this->dbUser, $this->dbPass);
    
    		if(!$dbLink) die("Could not connect to database: " . mysql_error());
    
    
    
    		mysql_select_db($this->dbName);
    
    
    
    		$query = "SELECT * FROM $this->dbUserTable WHERE userName = \"$this->userName\" LIMIT 1";
    
    		$result = mysql_query($query);
    
    
    
    		if(!$result) {
    
    			echo "Fail.";
    
    		} else {
    
    			$query = "UPDATE $this->dbUserTable SET userPass = \"$newPass\" WHERE userName = \"$this->userName\"";
    
    			$result = mysql_query($query);
    
    
    
    			if(!$result) {
    
    				echo "Fail";
    
    			} else {			
    
    				$this->userPass = $newPass;
    
    			}
    
    		}
    
    
    
    		mysql_close($dbLink);		
    
    	}
    
    
    
    }
    
    ?>

     

    Thank you for your help!!!  :D

     

     

  13. ajax_login.php gives a blank page...

     

    login.php

    <form method="post" action="ajax_login.php">
    
    Matrpicula: <input type="text" name="userEnrollment" maxlength="32"><br>
    
    Senha: <input type="password" name="userPass" maxlength="32"><br>
    
    <input type="submit">
    
    </form>

     

    ajax_login.php

    <?
    
    
    session_start();
    
    
    include 'class/network.php';
    
    
    
    $D = new network;
    
    
    
    $D->userEnrollment = mysql_real_escape_string($_POST['userEnrollment']);
    
    $D->userPassword = hash('sha512', $_POST['userPass']);
    
    
    
    $D->userLogin();
    
    echo "$_SESSION['userEnrollment']";
    
    
    
    ?>

     

    class/network.php

    <?
    
    
    
    class network {
    
    
    
    	var $userID,
    
    		$userEnrollment,
    
    		$userPass,
    
    		$dbHost,
    
    		$dbUser,
    
    		$dbName,
    
    		$dbPass,
    
    		$dbUserTable;
    
    
    
    	function dbInfo() {
    
    		$this->dbHost = 'localhost';
    
    		$this->dbUser = 'user';
    		$this->dbPass = 'pass';
    
    		$this->dbName = 'dbname';
    
    		$this->dbUserTable = 'usertable';
    
    	}
    
    
    
    	function userLogin() {
    
    		$dbLink = mysql_connect($this->dbHost, $this->dbUser, $this->dbPass);
    
    		if(!$dbLink) die("Could not connect to database: " . mysql_error());
    
    
    
    		mysql_select_db($this->dbName);
    
    
    
    		$query = "SELECT * FROM $this->dbUserTable WHERE userEnrollment = \"$this->userEnrollment\" AND userPass = \"$this->userPass\" LIMIT 1";
    
    		$result = mysql_query($query);
    
    
    
    		if(!$result) {
    
    			echo "Fail.";
    
    		} else {
    
    			$row = mysql_fetch_array($result))
    
    			session_regenerate_id();
    
    				$_SESSION['userEnrollment'] = $this->userEnrollment;
    
    			session_write_close();
    
    		}
    
    
    
    		mysql_close($dbLink);
    
    	}
    
    
    
    
    }
    
    ?>

  14. I am new to PHP.

     

    I developed this class, I wonder if there's anything wrong or that I can improve. I could not test it because I'm in school.

     

    Thanks in advance.

     

    <?
    
    
    
    class user {
    
    
    
    	var $userID,
    
    		$schoolID,
    
    		$userName,
    
    		$userPass,
    
    		$dbHost,
    
    		$dbUser,
    
    		$dbName,
    
    		$dbPass,
    
    		$dbUserTable;
    
    		$dbSchoolTable;
    
    
    
    	function dbInfo() {
    
    		$this->dbHost = 'localhost';
    
    		$this->dbUser = '';
    
    		$this->dbName = '';
    
    		$this->dbPass = '';
    
    		$this->dbUserTable = '';
    
    		$this->dbSchoolTable = '';
    
    	}
    
    
    
    	function registerUser($userName, $userPass) {
    
    		$dbLink = mysql_connect($this->dbHost, $this->dbUser, $this->dbPass);
    
    		if(!$dbLink) die("Could not connect to database: " . mysql_error());
    
    
    
    		mysql_select_db($this->dbName);
    
    
    
    		$query = "INSERT INTO $this->dbUserTable VALUES (NULL, \"$userName\", \"$userPass\")";
    
    		$result = mysql_query($query);
    
    
    
    		if(!$result) {
    			echo "Fail.";
    		} else {
    			$this->userID = mysql_insert_id();
    		}
    
    
    
    		mysql_close($dbLink);
    
    
    
    		$this->userName = $userName;
    
    		$this->userPass = $userPass;
    
    	}
    
    
    
    	function registerSchool($schoolName) {
    
    		$dbLink = mysql_connect($this->dbHost, $this->dbUser, $this->dbPass);
    
    		if(!$dbLink) die("Could not connect to database: " . mysql_error());
    
    
    
    		mysql_select_db($this->dbName);
    
    
    
    		$query = "INSERT INTO $this->dbSchoolTable VALUES (NULL, \"$schoolName\")";
    
    		$result = mysql_query($query);
    
    
    
    		if(!$result) {
    			echo "Fail.";
    		} else {
    			$this->schoolID = mysql_insert_id();
    		}
    
    
    
    		mysql_close($dbLink);
    
    
    
    		$this->schoolName = $schoolName;
    
    	}
    
    
    	function userLogin() {
    
    		$dbLink = mysql_connect($this->dbHost, $this->dbUser, $this->dbPass);
    
    		if(!$dbLink) die("Could not connect to database: " . mysql_error());
    
    
    
    		mysql_select_db($this->dbName);
    
    
    
    		$query = "SELECT * FROM $this->dbUserTable WHERE userName = \"$this->userName\" AND userPass = \"$this->userPass\" LIMIT 1";
    
    		$result = mysql_query($query);
    
    
    
    		if(!$result) {
    			echo "Fail.";
    		} else {
    			while($row = mysql_fetch_array($result)) {
    				session_start();
    				$_SESSION['userID'] = $row['userID'];
    				session_write_close();
    			}
    		}
    
    
    
    		mysql_close($dbLink);
    
    	}
    
    
    	function changePass($newPass) {
    
    		$dbLink = mysql_connect($this->dbHost, $this->dbUser, $this->dbPass);
    
    		if(!$dbLink) die("Could not connect to database: " . mysql_error());
    
    
    
    		mysql_select_db($this->dbName);
    
    		$query = "SELECT * FROM $this->dbUserTable WHERE userName = \"$this->userName\" LIMIT 1";
    
    		$result = mysql_query($query);
    
    		if(!$result) {
    			echo "Fail.";
    		} else {
    
    			$query = "UPDATE $this->dbUserTable SET userPass = \"$newPass\" WHERE userName = \"$this->userName\"";
    
    			$result = mysql_query($query);
    
    
    
    			if(!$result) {
    				echo "Fail";
    			} else {			
    				$this->userPass = $newPass;
    			}
    		}
    
    
    
    		mysql_close($dbLink);		
    
    	}
    
    
    
    }
    
    ?>

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.