Learning PHP, MySQL, JavaScript, CSS & HTML5 - Home Teachers India

Breaking

Welcome to Home Teachers India

The Passion for Learning needs no Boundaries

Translate

Tuesday, 25 October 2022

Learning PHP, MySQL, JavaScript, CSS & HTML5

Learning PHP, MySQL, JavaScript, CSS & HTML5


Table of Contents

Preface. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . xxi

1. Introduction to Dynamic Web Content. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 1

HTTP and HTML: Berners-Lee’s Basics 2

The Request/Response Procedure 2

The Benefits of PHP, MySQL, JavaScript, CSS, and HTML5 5

Using PHP 6

Using MySQL 7

Using a Program Editor 40

Using an IDE 41

Questions 43

3. Introduction to PHP. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 45

Incorporating PHP Within HTML 45

This Book’s Examples 47

The Structure of PHP 48

Using Comments 48

Basic Syntax 49

Variables 50

Operators 55

Variable Assignment 57

Multiple-Line Commands 60

Variable Typing 62

Constants 63

Predefined Constants 64

The Difference Between the echo and print Commands 64

Functions 65

Variable Scope 66

Questions 71

4. Expressions and Control Flow in PHP. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 73

Expressions 73

TRUE or FALSE? 73

Literals and Variables 75

Operators 76

Operator Precedence 77

Associativity 78

Relational Operators 80

Conditionals 84

The if Statement 84

The else Statement 85

The elseif Statement 87

The switch Statement 88

The ? Operator 91

Looping 92

while Loops 93

do ... while Loops 94

for Loops 95

Breaking Out of a Loop 97

The continue Statement 98

vi | Table of Contents

Implicit and Explicit Casting 98

PHP Dynamic Linking 99

Dynamic Linking in Action 100

Questions 101

5. PHP Functions and Objects. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 103

PHP Functions 104

Defining a Function 106

Returning a Value 106

Returning an Array 108

Passing by Reference 108

Returning Global Variables 110

Recap of Variable Scope 111

Including and Requiring Files 111

The include Statement 111

Using include_once 112

Using require and require_once 112

PHP Version Compatibility 113

PHP Objects 113

Terminology 114

Declaring a Class 115

Creating an Object 116

Accessing Objects 116

Cloning Objects 118

Constructors 119

PHP 5 Destructors 120

Writing Methods 120

Static Methods in PHP 5 121

Declaring Properties 122

Declaring Constants 122

Property and Method Scope in PHP 5 123

Static Properties and Methods 124

Inheritance 125

Questions 129

6. PHP Arrays. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 131

Basic Access 131

Numerically Indexed Arrays 131

Associative Arrays 133

Assignment Using the array Keyword 134

The foreach ... as Loop 135

Multidimensional Arrays 137

Table of Contents | vii

Using Array Functions 140

is_array 140

count 140

sort 140

shuffle 141

explode 141

extract 142

compact 143

reset 144

end 144

Questions 144

7. Practical PHP. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 147

Using printf 147

Precision Setting 148

String Padding 150

Using sprintf 151

Date and Time Functions 151

Date Constants 154

Using checkdate 154

File Handling 155

Checking Whether a File Exists 155

Creating a File 155

Reading from Files 157

Copying Files 158

Moving a File 158

Deleting a File 158

Updating Files 159

Locking Files for Multiple Accesses 160

Reading an Entire File 162

Uploading Files 162

System Calls 167

XHTML or HTML5? 169

Questions 169

8. Introduction to MySQL. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 171

MySQL Basics 171

Summary of Database Terms 172

Accessing MySQL via the Command Line 172

Starting the Command-Line Interface 173

Using the Command-Line Interface 177

MySQL Commands 178

viii | Table of Contents

Data Types 183

Indexes 192

Creating an Index 192

Querying a MySQL Database 198

Joining Tables Together 207

Using Logical Operators 209

MySQL Functions 209

Accessing MySQL via phpMyAdmin 210

Using phpMyAdmin 214

Questions 214

9. Mastering MySQL. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 217

Database Design 217

Primary Keys: The Keys to Relational Databases 218

Normalization 219

First Normal Form 220

Second Normal Form 222

Third Normal Form 224

When Not to Use Normalization 226

Relationships 227

One-to-One 227

One-to-Many 228

Many-to-Many 229

Databases and Anonymity 230

Transactions 230

Transaction Storage Engines 231

Using BEGIN 232

Using COMMIT 232

Using ROLLBACK 233

Using EXPLAIN 234

Backing Up and Restoring 235

Using mysqldump 235

Creating a Backup File 237

Restoring from a Backup File 239

Dumping Data in CSV Format 239

Planning Your Backups 240

Questions 240

10. Accessing MySQL Using PHP. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 241

Querying a MySQL Database with PHP 241

The Process 242

Creating a Login File 242

Table of Contents | ix

Connecting to MySQL 243

A Practical Example 248

The $_POST Array 251

Deleting a Record 252

Displaying the Form 252

Querying the Database 253

Running the Program 254

Practical MySQL 255

Creating a Table 255

Describing a Table 256

Dropping a Table 257

Adding Data 257

Retrieving Data 258

Updating Data 259

Deleting Data 260

Using AUTO_INCREMENT 260

Performing Additional Queries 262

Preventing SQL Injection 263

Using Placeholders 265

Preventing HTML Injection 266

Questions 268

11. Using the mysqli Extension. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 269

Querying a MySQL Database with mysqli 269

Creating a Login File 269

Connecting to MySQL 270

A Practical Example 274

Using mysqli Procedurally 276

Questions 277

12. Form Handling. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 279

Building Forms 279

Retrieving Submitted Data 281

register_globals: An Old Solution Hangs On 282

Default Values 283

Input Types 284

Sanitizing Input 291

An Example Program 292

What’s New in HTML5? 295

The autocomplete Attribute 295

The autofocus Attribute 295

The placeholder Attribute 296

x | Table of Contents

The required Attribute 296

Override Attributes 296

The width and height Attributes 297

Features Awaiting Full Implementation 297

The form Attribute 297

The list Attribute 297

The min and max Attributes 298

The step Attribute 298

The color Input Type 298

The number and range Input Types 298

Date and time Pickers 298

Questions 299

13. Cookies, Sessions, and Authentication. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 301

Using Cookies in PHP 301

Setting a Cookie 303

Accessing a Cookie 304

Destroying a Cookie 304

HTTP Authentication 304

Storing Usernames and Passwords 307

Salting 308

Using Sessions 312

Starting a Session 312

Ending a Session 315

Setting a Timeout 317

Session Security 317

Questions 320

14. Exploring JavaScript. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 323

JavaScript and HTML Text 324

Using Scripts Within a Document Head 325

Older and Nonstandard Browsers 325

Including JavaScript Files 326

Debugging JavaScript Errors 327

Using Comments 329

Semicolons 329

Variables 330

String Variables 330

Numeric Variables 330

Arrays 331

Operators 332

Arithmetic Operators 332

Table of Contents | xi

Assignment Operators 332

Comparison Operators 333

Logical Operators 333

Variable Incrementing and Decrementing 334

String Concatenation 334

Escaping Characters 334

Variable Typing 335

Functions 336

Global Variables 336

Local Variables 336

The Document Object Model 338

But It’s Not That Simple 340

Another Use for the $ Symbol 340

Using the DOM 341

Questions 342

15. Expressions and Control Flow in JavaScript. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 343

Expressions 343

Literals and Variables 344

Operators 345

Operator Precedence 346

Associativity 346

Relational Operators 347

The with Statement 350

Using onerror 351

Using try ... catch 352

Conditionals 353

The if Statement 353

The else Statement 353

The switch statement 354

The ? Operator 355

Looping 356

while Loops 356

do ... while Loops 357

for Loops 357

Breaking Out of a Loop 358

The continue Statement 359

Explicit Casting 360

Questions 360

16. JavaScript Functions, Objects, and Arrays. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 363

JavaScript Functions 363

xii | Table of Contents

Defining a Function 363

The arguments Array 364

Returning a Value 365

Returning an Array 367

JavaScript Objects 368

Declaring a Class 368

Creating an Object 369

Accessing Objects 370

The prototype Keyword 370

JavaScript Arrays 372

Numeric Arrays 373

Associative Arrays 374

Multidimensional Arrays 375

Using Array Methods 376

Questions 380

17. JavaScript and PHP Validation and Error Handling. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 381

Validating User Input with JavaScript 381

The validate.html Document (Part One) 382

The validate.html Document (Part Two) 384

Regular Expressions 387

 388

Using Regular Expressions in JavaScript 395

Using Regular Expressions in PHP 396

Redisplaying a Form After PHP Validation 397

Questions 403

18. Using Ajax. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 405

What Is Ajax? 405

Using XMLHttpRequest 406

Your First Ajax Program 408

Using GET Instead of POST 413

Sending XML Requests 415

Using Frameworks for Ajax 420

Questions 421

19. Introduction to CSS. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 423

Importing a Style Sheet 424

Importing CSS from Within HTML 424

Embedded Style Settings 425

Using IDs 425

Using Classes 425

Table of Contents | xiii

Using Semicolons 426

CSS Rules 426

Multiple Assignments 426

Using Comments 427

Style Types 428

Default Styles 428

User Styles 428

External Style Sheets 429

Internal Styles 429

Inline Styles 430

CSS Selectors 430

The Type Selector 430

The Descendant Selector 430

The Child Selector 431

The ID Selector 432

The Class Selector 433

The Attribute Selector 434

The Universal Selector 434

Selecting by Group 435

The CSS Cascade 435

Style Sheet Creators 436

Style Sheet Methods 436

Style Sheet Selectors 437

Calculating Specificity 437

The Difference Between Div and Span Elements 439

Measurements 440

Fonts and Typography 442

font-family 442

font-style 443

font-size 443

font-weight 444

Managing Text Styles 444

Decoration 445

Spacing 445

Alignment 446

Transformation 446

Indenting 446

CSS Colors 447

Short Color Strings 447

Gradients 448

Positioning Elements 449

Absolute Positioning 449

xiv | Table of Contents

Relative Positioning 450

Fixed Positioning 450

Pseudo-Classes 452

Shorthand Rules 454

The Box Model and Layout 454

Setting Margins 455

Applying Borders 457

Adjusting Padding 458

Object Contents 459

Questions 459

20. Advanced CSS with CSS3. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 461

Attribute Selectors 461

The ^ Operator 462

The $ Operator 462

The * Operator 463

The box-sizing Property 463

CSS3 Backgrounds 463

The background-clip Property 464

The background-origin Property 465

The background-size Property 466

Multiple Backgrounds 467

CSS3 Borders 469

The border-color Property 469

The border-radius Property 469

Box Shadows 472

Element Overflow 473

Multicolumn Layout 473

Colors and Opacity 475

HSL Colors 475

HSLA Colors 476

RGB Colors 476

RGBA Colors 477

The opacity Property 477

Text Effects 477

The text-shadow Property 477

The text-overflow Property 478

The word-wrap Property 479

Web Fonts 479

Google Web Fonts 480

Transformations 481

3D Transformations 483

Table of Contents | xv

Transitions 483

Properties to Transition 484

Transition Duration 484

Transition Delay 484

Transition Timing 485

Shorthand Syntax 485

Questions 487

21. Accessing CSS from JavaScript. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 489

Revisiting the getElementById Function 489

The O function 489

The S Function 490

The C Function 491

Including the Functions 492

Accessing CSS Properties from JavaScript 493

Some Common Properties 494

Other Properties 495

Inline JavaScript 497

The this Keyword 497

Attaching Events to Objects in a Script 498

Attaching to Other Events 499

Adding New Elements 500

Removing Elements 501

Alternatives to Adding and Removing Elements 502

Using Interrupts 503

Using setTimeout 503

Canceling a Timeout 504

Using setInterval 504

Using Interrupts for Animation 506

Questions 508

22. Introduction to HTML5. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 509

The Canvas 510

Geolocation 511

Audio and Video 513

Forms 514

Local Storage 515

Web Workers 515

Web Applications 515

Microdata 516

Summary 516

xvi | Table of Contents

Questions 516

23. The HTML5 Canvas. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 517

Creating and Accessing a Canvas 517

The toDataURL Function 519

Specifying an Image Type 521

The fillRect Method 521

The clearRect Method 521

The strokeRect Method 522

Combining These Commands 522

The createLinearGradient Method 523

The addColorStop Method in Detail 525

The createRadialGradient Method 526

Using Patterns for Fills 528

Writing Text to the Canvas 530

The strokeText Method 530

The textBaseLine Property 531

The font Property 531

The textAlign Property 531

The fillText Method 532

The measureText Method 533

Drawing Lines 533

The lineWidth Property 533

The lineCap and lineJoin Properties 533

The miterLimit Property 535

Using Paths 536

The moveTo and LineTo Methods 536

The stroke Method 537

The rect Method 537

Filling Areas 537

The clip Method 539

The isPointInPath Method 542

Working with Curves 543

The arc Method 543

The arcTo Method 546

The quadraticCurveTo Method 547

The bezierCurveTo Method 548

Manipulating Images 549

The drawImage Method 549

Resizing an Image 550

Selecting an Image Area 550

Copying from a Canvas 552

Table of Contents | xvii

Adding Shadows 552

Editing at the Pixel Level 554

The getImageData Method 554

The data Array 555

The putImageData Method 557

The createImageData Method 557

Advanced Graphical Effects 558

The globalCompositeOperation Property 558

The globalAlpha Property 561

Transformations 561

The scale Method 561

The save and restore Methods 562

The rotate Method 562

The translate Method 564

The transform Method 565

The setTransform Method 567

Summary 567

Questions 567

24. HTML5 Audio and Video. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 569

About Codecs 570

The <audio> Element 571

Supporting Non-HTML5 Browsers 573

The <video> Element 574

The Video Codecs 575

Supporting Older Browsers 578

Summary 580

Questions 580

25. Other HTML5 Features. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 581

Geolocation and the GPS Service 581

Other Location Methods 582

Geolocation and HTML5 583

Local Storage 586

Using Local Storage 587

The localStorage Object 587

Web Workers 589

Offline Web Applications 591

Drag and Drop 593

Cross Document Messaging 595

Microdata 598

Other HTML5 Tags 601

xviii | Table of Contents

Summary 601

Questions 602

26. Bringing It All Together. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 603

Designing a Social Networking Site 603

On the Website 604

functions.php 604

The Functions 605

header.php 607

setup.php 608

index.php 610

signup.php 610

Checking for Username Availability 611

Logging In 611

checkuser.php 614

login.php 615

profile.php 617

Adding the “About Me” Text 618

Adding a Profile Image 618

Processing the Image 618

Displaying the Current Profile 619

members.php 622

Viewing a User’s Profile 622

Adding and Dropping Friends 622

Listing All Members 622

friends.php 625

messages.php 628

logout.php 631

styles.css 632

javascript.js 636

A. Solutions to the Chapter Questions. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 639

B. Online Resources. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 659

C. MySQL’s FULLTEXT Stopwords. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 663

D. MySQL Functions. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 667

Index. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 677


 

Using Code Examples Supplemental material (code examples, exercises, etc.) is available at http://lpmj.net. This book is here to help you get your job done. In general, if example code is offered with this book, you may use it in your programs and documentation. You do not need to contact us for permission unless you’re reproducing a significant portion of the code. For example, writing a program that uses several chunks of code from this book does not require permission. Selling or distributing a CD-ROM of examples from O’Reilly books does require permission. Answering a question by citing this book and quoting example code does not require permission. Incorporating a significant amount of ex‐ ample code from this book into your product’s documentation does require permission. We appreciate, but do not require, attribution. An attribution usually includes the title, author, publisher, and ISBN. For example: “Learning PHP, MySQL, JavaScript, CSS & HTML5, Third Edition, by Robin Nixon. Copyright 2014 Robin Nixon, 978-1-4919-4946-7.” If you feel your use of code examples falls outside fair use or the permission given here, feel free to contact us at permissions@oreilly.com. We’d Like to Hear from You Every example in this book has been tested on various platforms, but occasionally you may encounter problems—for example, if you have a nonstandard installation or a different version of PHP. The information in this book has also been verified at each step of the production process. However, mistakes and oversights can occur and we will gratefully receive details of any you find, as well as any suggestions you would like to make for future editions. You can contact the author and editors at: O’Reilly Media, Inc. 1005 Gravenstein Highway North Sebastopol, CA 95472 (800) 998-9938 (in the United States or Canada) (707) 829-0515 (international or local) (707) 829-0104 (fax) We have a web page for this book, where we list errata, examples, and any additional information. You can access this page at http://bit.ly/lpmjch_3e. There is also a companion website to this book at http://lpmj.net, where you can down‐ load all the examples from this book in a single zip file. To comment or ask technical questions about this book, send email to bookques tions@oreilly.com. xxiv | Preface For more information about our books, courses, conferences, and news, see our website at http://www.oreilly.com. Find us on Facebook: http://facebook.com/oreilly Follow us on Twitter: http://twitter.com/oreillymedia Watch us on YouTube: http://www.youtube.com/oreillymedia Safari® Books Online Safari Books Online (www.safaribooksonline.com) is an ondemand digital library that delivers expert content in both book and video form from the world’s leading authors in technology and business. Technology professionals, software developers, web designers, and business and crea‐ tive professionals use Safari Books Online as their primary resource for research, prob‐ lem solving, learning, and certification training. Safari Books Online offers a range of product mixes and pricing programs for organi‐ zations, government agencies, and individuals. Subscribers have access to thousands of books, training videos, and prepublication manuscripts in one fully searchable database from publishers like O’Reilly Media, Prentice Hall Professional, Addison-Wesley Pro‐ fessional, Microsoft Press, Sams, Que, Peachpit Press, Focal Press, Cisco Press, John Wiley & Sons, Syngress, Morgan Kaufmann, IBM Redbooks, Packt, Adobe Press, FT Press, Apress, Manning, New Riders, McGraw-Hill, Jones & Bartlett, Course Technol‐ ogy, and dozens more. For more information about Safari Books Online, please visit us online. Acknowledgments I would like to once again thank my editor, Andy Oram, and everyone who worked so hard on this book, including Albert Wiersch for his comprehensive technical review, Kristen Brown for overseeing production, Rachel Monaghan for her copyediting, Jas‐ mine Kwityn for proofreading, Robert Romano for his original illustrations, Rebecca Demarest for her new illustrations, David Futato for interior design, Lucie Haskins for creating the index, Karen Montgomery for the original sugar glider front cover design, Randy Comer for the latest book cover, and everyone else too numerous to name who submitted errata and offered suggestions for this new edition. 

No comments:

Post a Comment

Thank you for Contacting Us.

Post Top Ad